9951 explained code solutions for 126 technologies


nginx-luaHow to parse JSON


init_by_lua_block  {
  cjson = require("cjson")
}

server {
  location / {
    content_by_lua_block {
      local json = '{"name":"Donald"}'
      local tbl = cjson.decode(json)
      ngx.say(tbl['name'])
    }
  }
}ctrl + c
init_by_lua_block

nginx-lua module directive to run specified Lua code on server startup

require("cjson")

load module to manipulate JSON

content_by_lua_block

nginx-lua module directive to specify block of Lua code

'{"name":"Donald"}'

sample JSON string to parse

cjson.decode

parses given JSON string into table

tbl['name']

get name key value from tbl table which has parsed data from JSON

ngx.say

output given text to client