server {

server_name api; listen 8082; lua_need_request_body on; location /json/add/{ root /home/ivan/json/ ; access_by_lua_block{ local cjson = require("cjson") ngx.req.read_body() local text = ngx.var.request_body local value = cjson.new().decode(text) ngx.say(text) ngx.say(type(value)) ngx.say(table.getn(value)) for k,v in ipairs(value) do ngx.say(k) ngx.say(table.getn(v)) for _, vv in ipairs(v) do ngx.say(_) ngx.say(vv) end end } } } 

send a request to curl -H "Content-Type: application / json" -X POST -d '[{"username": "xyz"}, {"password": "xyz"}]' http: // localhost: 8082 / json / add /

answer

[{"username": "xyz"}, {"password": "xyz"}] table 2 1 0 2 0

How to get username, password?

    1 answer 1

    The table.getn() method (by the way, you need to use the # operator instead) works only with "real" arrays (with numeric keys). You can get a username like this: v["username"] or v.username

    Or replace pairs in the internal cycle of ipairs :

     for kk, vv in pairs(v) do ngx.say(kk) ngx.say(vv) end