0 votes

print(typeof({1:0})) # prints 18 i.e TYPE_DICTIONARY = 18 this is okay
print(parse_json("{1:0}")) # prints Null and this is not okay
print(parse_json("(1:0)")) # prints Null

How can i get dictionary with method parse_json()?

in Engine by (22 points)
edited by

2 Answers

0 votes
Best answer

Hi, try with this:


Keys must be string as @Zylann said. Notice also the use of quotes... it won't work if you change them like this: print(parse_json("{'1':0}"))

by (3,505 points)
selected by

Wow, it works! You are my savior. (i confused in quotes :/)

+1 vote

parse_json("{1:0}") returns null because "{1:0}" is not valid JSON. The specification does not allow numbers as keys, only strings.

parse_json("(1:0)") is also invalid not only for the same reason, but also because the JSON specification does not have any ().

If you want to save a container which has numbers as keys, use an array, or use a dictionary where keys are converted to string.

by (29,120 points)

Can you give an example with parse_json(), returning a dictionary pls?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.