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"}')
pokstad
  • 3,411
  • 3
  • 30
  • 39
  • 1
    Giving your question the benefit of the doubt, could you give me an example of JSON that could be python code? – orkutWasNotSoBad Jul 08 '11 at 16:44
  • @IRegretable: I can copy and paste the examples from http://json.org/example.html directly into the Python interpreter and get back a valid dictionary. – FogleBird Jul 08 '11 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 – Jim Deville Jul 08 '11 at 16:49
  • 3
    @James: This question has nothing to do with the *parsing* of JSON, but rather with the *direct consumption* of it. – Ignacio Vazquez-Abrams Jul 08 '11 at 16:50
  • Understood, but if python parses differently than the spec, then it affects the answer to the main question "Can all JSON objects directly convert to Python without error". Coercion/mutation is an error in my mind – Jim Deville Jul 08 '11 at 16:53
  • I'm concerned about how the answer to this question will be used. `eval(jsondata)` isn't much faster than `simplejson.loads(jsondata)` and far less secure. Why do you have this question? – SingleNegationElimination Jul 08 '11 at 17:53
  • It's definitely insecure with an untrusted source of JSON data. I copy and paste a lot of JSON between Python scripts for small automation tasks so my code is isolated from external threats. More of a question concerning convenience than speed. – pokstad Jul 08 '11 at 17:59
  • 2
    There's also `ast.literal_eval` which is much safer than `eval`. – FogleBird Jul 08 '11 at 18:40
  • +1 FogleBird, I wonder if you can implement a safe and fast JSON parser using this method combined with defining variables true, false, and null. – pokstad Jul 08 '11 at 20:43
  • You may want to look at [this question](http://stackoverflow.com/questions/5584943/whats-the-difference-between-python-objects-and-json-objects/5585962#5585962). – brandizzi Jul 08 '11 at 21:32

2 Answers2

20

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
pokstad
  • 3,411
  • 3
  • 30
  • 39
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • 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. – pokstad Jul 08 '11 at 20:39
  • 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."}`. – Jasmijn Jul 08 '11 at 20:50
  • 6
    A regex? Now you have two problems! ;) – Jasmijn Aug 12 '11 at 06:28