Issue with dictionary parsing (parse_json())

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By OrdinaryGuy

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()?

:bust_in_silhouette: Reply From: Zylann

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.

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

OrdinaryGuy | 2019-01-21 14:01

:bust_in_silhouette: Reply From: p7f

Hi, try with this:

print(parse_json('{"1":0}'))

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}"))

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

OrdinaryGuy | 2019-01-21 14:41