You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to openresty-en
I am trying to parse some json and for some reason the table returned from decode is always nil. Maybe something wrong with the json string but it seems correct. Can anyone tell me what I am doing wrong ?
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to openresty-en
There are a couple of issues with your code. The first is that you're calling say(cjson.encode(value)) instead of ngx.say(cjson.encode(value))
The second is that when you define json_text, you have put the json object inside a json array (the inner most square brackets). That means to reference "message" you actually have to reference the the position in the array (lua starts and 1 not 0) first. Try this:
local cjson = require("cjson")
local json_text = [[ [{ "message": "somewhere over the rainbow", "key": "7F02D18E-88E7-486D-B51F-550118491CB1"}] ]]
local value = cjson.decode(json_text)
ngx.say(cjson.encode(value))
ngx.say(value[1]["message"])
or alternatively:
local cjson = require("cjson")
local json_text = [[ { "message": "somewhere over the rainbow", "key": "7F02D18E-88E7-486D-B51F-550118491CB1"} ]]