So my string of data I am parsing is a validated json string that starts and ends with curly braces {}. When I check JSONParseResult that is returned, JSONParseResult.error is OK. When I look at JSONParseResult.result, typeof tells me it is a string and this is where I am having trouble. I want it to be an array, or Dicitonary, or JSON object? So I can read the data. Otherwise I have to parse this result string myself which I will do if I have to but it seems like json_parse should be somewhat more helpful in the regard, the whole point of using it in fact. I really feel like I am missing something because searching Google is not producing an answer.
Trying to read data returned from parse_json() (aka JSON.parse(), I believe they do the same thing).
- Edited
Which Godot version are you using?
Also, can you post the JSON string in question here? Or, better, upload a minimal reproduction project so other people can try to troubleshoot this.
You should be getting a JSONParseResult. Which in your case would store a Dictionary in "result".
https://docs.godotengine.org/en/stable/classes/class_jsonparseresult.html#class-jsonparseresult
Can you post some code of how your are loading the Json? There is not enough info to help.
- Edited
OK, thank you for the Dictionary hint. So apparently JSON.parse returns a JSONParseResult. After checking no errors, I had to then call get_result() method of JSONParseResult. This returned a string. Then I had to call parse_json() passing this string as a parameter. This is what finally resulted in the Dictionary variant I needed. Yay! Code below:
data string passed (downloaded from cloud):
str_card_dict = "{\"id\":0,\"x\":388.114319,\"y\":285.681793,\"to_lines\": [{\"0\":\"N\"},{\"1\":\"N\"},{\"2\":\"N\"},{\"3\":\"N\"},{\"4\":\"N\"},{\"5\":\"N\"},{\"6\":\"N\"},{\"7\":\"N\"}],\"from_lines\": [{\"0\":\"[]\"},{\"1\":\"[]\"},{\"2\":\"[]\"},{\"3\":\"[]\"},{\"4\":\"[]\"},{\"5\":\"[]\"},{\"6\":\"[]\"},{\"7\":\"[]\"}]}"
var result_json = JSON.parse(str_card_dict) # data string from cloud to be converted
#var result = {}
if result_json.error != OK: # If parse has errors
print("Error: ", result_json.error)
print("Error Line: ", result_json.error_line)
print("Error String: ", result_json.error_string)
else: # If parse OK
# result_json typeof = JSONParseResult object
print("result_json of JSON.parse typeof=", typeof(result_json))
var json_data = result_json.get_result() # should return a Variant of type string
print("json_data typeof=", typeof(json_data))
#print(json_data)
var result_dict = parse_json(json_data)
print("result_dict typeof =", typeof(result_dict))
print(result_dict)
var keys = result_dict.keys()
print(keys)
That's strange. I'm not sure why you would have to parse the result twice.
- Edited
If the JSON data that's passed to JSON.parse() has embedded quotes surrounding it, would that explain it?
Raw JSON data is normally not enclosed in quotes.