Difference Between Str(dict) And Json.dumps(dict)
Solution 1:
str(aDictionary)
(as well as repr(aDictionary)
) produces a Python representation of your dictionary. This representation is helpful for debugging and nothing more. For the built-in types such as dictionaries and strings, you'll be given a representation that is valid Python syntax. Python syntax and JSON syntax may look similar, but are not the same thing.
- Python uses single and double quotes for string literals. JSON only allows double quotes.
- Python Unicode strings use
\<single letter>
,\xhh
,\uhhhh
and\Uhhhhhhhh
escape sequences to encode Unicode codepoints; the latter form is used for non-BMP codepoints. JSON uses a smaller range of\<single letter>
escapes as well as the\uhhhh
form, and encodes UTF-16 surrogate pairs for non-BMP codepoints (so two\uhhhh
sequences per codepoint). - Python uses
None
objects, JSON usesnull
to as a special "doesn't exist" sentinel value. - Python uses
True
andFalse
as boolean values, JSON usestrue
andfalse
. - Python dictionary keys can be any hashable type, JSON only supports strings.
So str(dictionary)
will not produce valid JSON output, most of the time; only if all your keys and values are BMP strings with at least one single quote in the value could you end up with a document that can also be parsed as valid JSON.
In terms of your specific example, note that str(aDictionary)
produces a document with single quotes; json.loads()
can't load this document as that's not valid JSON:
>>> import json
>>> aDictionary = {"first": 42, "second":21}
>>> str(aDictionary)
"{'first': 42, 'second': 21}">>> json.loads(str(aDictionary))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Never use str(pythonobject)
as a serialisation. Although the ast.literal_eval()
function can load a reasonable number of Python built-in types from a string representation, it is a lot slower than using JSON for the same job, or a more sophisticated data persistence format if JSON doesn't fit your needs.
Post a Comment for "Difference Between Str(dict) And Json.dumps(dict)"