function _M.transform_json_body()

in kong/kong/plugins/response-transformer/body_transformer.lua [92:162]


function _M.transform_json_body(conf, buffered_data)
  local json_body = read_json_body(buffered_data)
  if json_body == nil then
    return
  end

  
  for _, name in iter(conf.remove.json) do
    json_body[name] = nil
  end

  
  for i, name, value in iter(conf.replace.json) do
    local v = cjson.encode(value)
    if v and sub(v, 1, 1) == [["]] and sub(v, -1, -1) == [["]] then
      v = gsub(sub(v, 2, -2), [[\"]], [["]]) 
    end

    v = v and gsub(v, [[\/]], [[/]]) 

    if conf.replace.json_types then
      local v_type = conf.replace.json_types[i]
      v = cast_value(v, v_type)
    end

    if json_body[name] and v ~= nil then
      json_body[name] = v
    end
  end

  
  for i, name, value in iter(conf.add.json) do
    local v = cjson.encode(value)
    if v and sub(v, 1, 1) == [["]] and sub(v, -1, -1) == [["]] then
      v = gsub(sub(v, 2, -2), [[\"]], [["]]) 
    end

    v = v and gsub(v, [[\/]], [[/]]) 

    if conf.add.json_types then
      local v_type = conf.add.json_types[i]
      v = cast_value(v, v_type)
    end

    if not json_body[name] and v ~= nil then
      json_body[name] = v
    end

  end

  
  for i, name, value in iter(conf.append.json) do
    local v = cjson.encode(value)
    if v and sub(v, 1, 1) == [["]] and sub(v, -1, -1) == [["]] then
      v = gsub(sub(v, 2, -2), [[\"]], [["]]) 
    end

    v = v and gsub(v, [[\/]], [[/]]) 

    if conf.append.json_types then
      local v_type = conf.append.json_types[i]
      v = cast_value(v, v_type)
    end

    if v ~= nil then
      json_body[name] = append_value(json_body[name],v)
    end
  end

  return cjson.encode(json_body)
end