7

This is a simple questions that is really only a footnote in something I am writing:

Is any valid JSON not also valid Python?

I know the converse is true, i.e. Python data structures and scalars allow a variety of constructs that are not JSON. But for the most part, JSON seems to be a subset of Python syntax for defining (some) data structures.

The obvious stuff is covered. Strings are strings. Ints are ints. JSON "numbers" are read as Python floats (although RFC 8259 does not mandate that interpretation vs. fixed point, for example). Dicts are dicts. Lists are lists.

But maybe something in some obscure corner violates the subset relationship. For example, is there anything in the encoding of Unicode outside the BMP that is directly incompatible? Or maybe within Unicode surrogate pairs?

Or maybe something with numbers where some large number of digits after the decimal would be technically valid JSON but not Python? (I don't think so, but just trying to think of scenarios).

5
  • 1
    JSON is JSON, Python is Python. The syntax, while similar, are not the same. That's why you need json module to load JSON data instead of simply reading the text. Commented May 23, 2020 at 19:25
  • Can you show an example of a string that is valid JSON but is not a valid Python expression. I absolutely know the security reasons to use the json module, etc. This is more pedantic. Show me a string such that: python json.loads(jstr) != eval(jstr) Or where the eval() raises an exception but json.loads() does not. Commented May 23, 2020 at 19:28
  • 1
    I'm having a hard time understanding how you can compare JSON and Python code though. You should be more specific maybe? What exactly are you comparing? Commented May 23, 2020 at 19:28
  • {1: true} will immediately be an invalid Python. Think about what you are asking. Besides, Python has strict indentation requirements where JSON doesn't care for. One is a data interchange format, the other is a programming language. You're comparing apples to oranges. Commented May 23, 2020 at 19:30
  • @r.ook That is a good example you present. But Python has no indentation requirement for defining dicts or lists, so that is not relevant. My question was specific only to wondering about what Python calls "displays", which is more-or-less the literals that define data structures. Commented May 24, 2020 at 3:03

2 Answers 2

12

The most obvious thing is that true, false and null don't exist in Python. They are called True, False and None.

In addition, \/ in strings is interpreted as / in json and as \/ in Python:

>>> a = '"\/"'
>>> print(a)
"\/"
>>> print(eval(a))
\/
>>> print(json.loads(a))
/
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That's exactly what I wanted... and felt like I already knew but was spacing on. Are true/false/null the complete set of reserved names? RFC 8259, section 3, seems to indicate that. I know many extensions also reserve NaN , but that's not in the spec itself.
@DavidMertz I think so. A short JSON reference can be found here: json.org
-2

Yes, you are correct, every valid JSON can be handled in Python. Python is a complete language, and JSON is a way of storing data (serialisation maybe?). Generally a language will support everything a JSON object can represent.

There would be different representation for sure like true in JSON is True in Python.

Since, JSON is way of storing data, and we can also pass it around HTTP requests, which are always processed by some server side language, which is expected to handle the JSON object.

1 Comment

Everything you wrote is correct, but I don't think it is what the question is talking about.

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.