I'm receiving this JSON in the POST data:
{
"foo":{
"hi": "there",
"hello": "world"
}
}
In Lua, I want to get foo in order to save it in Redis, so it'd be great to save it as an string.
How can I get it?
You'll need to install json-lua or lua-cjson packages first. Then, parse the JSON response (received as string
) and it gets converted to a table.
Using pairs()
you can iterate over key-values of the table generated above.
OpenResty already bundles a fork of lua-cjson
with it.
If the string is s
, then this extracts the value of foo
:
print(s:match('"foo"%s*:%s*(%b{})'))