9951 explained code solutions for 126 technologies


nginx-luaHow to encode to JSON


init_by_lua_block  {
  cjson = require("cjson")
}

server {
  location / {
    content_by_lua_block {
      local tbl = {name='Donald'}
      local json = cjson.encode(tbl)
      ngx.say(json)
    }
  }
}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 table to convert to JSON string

cjson.encode

encodes given Lua table to JSON string

ngx.say

output given text to client