[SOLVED] Convert JSON to Lua

So i’ve been wondering if it’s possible to convert a JSON (LUA string) to a Lua table.

For example:

String to convert:

local objects = "{'food':{'burger', 'carrot', 'nugget'}, 'animals':{'okapi', 'unicorn' = false, 'human'}}"

Table to convert to:

local objects = {['food']={'burger', 'carrot', 'nugget'}, ['animals'] = {'okapi', ['unicorn'] = false, 'human'}}

Don’t blame me if the syntax is wrong, the thing is you get the point.

Any ideas would be appreciated. Thanks in advance :slight_smile:

Edit:
I found out a function called JSON.parse in JavaScript, but I still need the one for Lua.

Ha. Was literally just Googling this myself yesterday.

json.decode and json.encode should do what you want.

That’s exactly what I was looking at right now lol. I’m testing it and I should be fine with it. Thanks

Solved!

The correct way to do it is as follows:

local jsonString = '{"foo":"bar"}'

print(json.decode(jsonString).foo) -- Output: bar