19

JSON is very similar to Python syntax. Can all JSON objects directly convert to Python without error?

Example

The following is a valid JSON object:

// Valid JSON
{"foo":"bar"}

This object will directly translate to a Python dictionary with key "foo" and value "bar":

# Python
json_dict = eval('{"foo":"bar"}')
11
  • 1
    Giving your question the benefit of the doubt, could you give me an example of JSON that could be python code? Commented Jul 8, 2011 at 16:44
  • @IRegretable: I can copy and paste the examples from json.org/example.html directly into the Python interpreter and get back a valid dictionary. Commented Jul 8, 2011 at 16:46
  • I don't know python well enough to know for sure, but there may be cases where python's parsing of the json string differ from the json spec Commented Jul 8, 2011 at 16:49
  • 3
    @James: This question has nothing to do with the parsing of JSON, but rather with the direct consumption of it. Commented Jul 8, 2011 at 16:50
  • 2
    There's also ast.literal_eval which is much safer than eval. Commented Jul 8, 2011 at 18:40

2 Answers 2

21

No. In particular, true, false, and null are not Python, although they do have direct equivalents in Python (True, False, and None respectively).

// Valid JSON
{"sky_is_blue":true}

But when used in Python...

# Python
>>> json_dict = eval('{"sky_is_blue":true}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'true' is not defined
Sign up to request clarification or add additional context in comments.

8 Comments

+1 That's good to know. Is that the full extent of incompatibilities or are there a lot of other Javascript specific stuff that Python doesn't like?
Strings can get a bit fuzzy depending on which way you interpret the specs (or lack thereof), but these three are the big differences.
@FogleBird: No you haven't; that's not JSON.
As far as I know, these are the only strict JSON values which aren't in Python. Values like NaN and Infinity aren't strictly JSON, but may be found in JSON strings (indeed, Python's json module will use them unless specifically told not to).
@Ignacio: You're right. I guess people abuse JSON like HTML sometimes.
|
6

This question has been answered (and the answer accepted) already, but I'd like to point out that the problem of true, false and null not being Python can be overcome by using the following code before evaluating JSON:

true = True
false = False
null = None

Of course, a JSON-parser is still better.

3 Comments

I wonder if this combined with the ast.literal_eval mentioned by FogleBird above could be used to construct a fast pure Python JSON parser.
Not really, as variables are not literals and thus not allowed... although one could do a string.replace('true', 'True') etc. before passing to ast.literal_eval. Of course, that does nasty things to {"Quote": "This sentence is false."}.
A regex? Now you have two problems! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.