0

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?

loar
  • 1,505
  • 1
  • 15
  • 34

2 Answers2

0

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.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0

If the string is s, then this extracts the value of foo:

print(s:match('"foo"%s*:%s*(%b{})'))
lhf
  • 70,581
  • 9
  • 108
  • 149