Parse JSON with a bunch of variables

Hey everyone! I am trying to get stuff from a JSON. I Decoded it and I am trying to access variable body_totally_main (see the Tree below)

I have tried:

local JSON = "*insert long JSON here*"
local decode = game:GetService("HttpService"):JSONDecode(JSON)

-- Tried:
print(decode.message.body.body_main.body_totally_main)
-- Also Tried
print(decode.body_main[7].body_totally_main)

What am I supposed to do?

Could you link the json?

Also you should print what’s decoded to see exactly what you’re working with. Most likely, you’ll need to do something like this:

print(decode[1].message.body.body_main.body_totally_main)

Sure here is the JSON:

{“message”:{“header”:{“status_code”:200,“execute_time”:0.012158870697021},“body”:{“body_main”:{“id”:23987514,“number”:0,“body_totally_main”:“THING TO GET”,“tracking_url”:“kx_1I1c8oPLwkt3cCLvi9Qq4REbQRmIyi_rS26L5r9de5hS_bSA4BbJ0Tn8HrEb80oPSTM09tJVcZUATadtY8zyKAWUeFickPsjngOIKpyYxn-sQFH4_OJII9OS21EHljHsqXAmYjIbVwkB8MxyGl6jz6ZmH8PRfKULu_jK2OJV1k/”,“copyright”:“Powered by www.nothing.com. This is NOT for Commercial use.”,“updated_time”:“2022-07-28T23:41:59Z”}}}}

Keep in mind that I removed private information from the JSON.

Whatever you removed seems to have made it unable to be parsed.

No, I removed some stuff that I posted (eg. the api key). I am still using the full json

Regardless, I’m unable to parse it to begin with.

Yep. Getting this error: String literal contains malformed escape sequence. I used the full json.


‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

I will get rid of stuff to see if it gets fixed.

jsonlint says it is valid.

EDIT: Gonna try to parse this in python to confirm it is good.

Parsed in Python. It is a valid JSON

The issue was the quotes:

local json = [[{“message”:{“header”:{“status_code”:200,“execute_time”:0.012158870697021},“body”:{“body_main”:{“id”:23987514,“number”:0,“body_totally_main”:“THING TO GET”,“tracking_url”:“kx_1I1c8oPLwkt3cCLvi9Qq4REbQRmIyi_rS26L5r9de5hS_bSA4BbJ0Tn8HrEb80oPSTM09tJVcZUATadtY8zyKAWUeFickPsjngOIKpyYxn-sQFH4_OJII9OS21EHljHsqXAmYjIbVwkB8MxyGl6jz6ZmH8PRfKULu_jK2OJV1k/”,“copyright”:“Powered by www.nothing.com. This is NOT for Commercial use.”,“updated_time”:“2022-07-28T23:41:59Z”}}}}]]
json = string.gsub(json,'“','"')
json = string.gsub(json,'”','"')
local httpService = game:GetService('HttpService')
local decoded = httpService:JSONDecode(json)
print(decoded)
print(decoded.message.body.body_main.body_totally_main)
2 Likes

Thank you! It works! Don’t know why python can do it, but Lua struggled.

It may be a Roblox specific thing. Those quotes are specifically for things like word documents for the fancy open/close quotes. Roblox probably doesn’t support those as normal quotes as they have a different ascii.

To be more specific, it’s the fault of the IDE probably.

2 Likes