What is the difference between json.dumps and str ()?

What is the difference between json.dumps(..)and str(..)?

Don't they convert json to string?

+4
source share
1 answer

No. In fact, in (I believe most) Python implementations, it str(object)wraps the string in single quotes, which is not valid JSON.

Example:

In [17]: print str({"a": 1})
{'a': 1}

str(boolean) JSON is also invalid:

In [18]: print str(True)
True

__str__however, it can be overridden in custom classes to ensure that objects return JSON representations themselves.

+4
source

Source: https://habr.com/ru/post/1529866/


All Articles