What is the difference between the output of str(a dictionary)
and json.dumps(a dictionary)
? If I print them, they look the same. Their attributes (as per dir
) also seem to be identical (see below). Yet I know there must be a difference. Could you please explain it to me?
import json
aDictionary= {"first": 42, "second":21}
s = str(aDictionary)
j = json.dumps(aDictionary)
s == j # returns false
dir(s) == dir(j) # returns true
json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.
JavaScript Object Notation (JSON) is a standardized format commonly used to transfer data as text that can be sent over a network. It's used by lots of APIs and Databases, and it's easy for both humans and machines to read. JSON represents objects as name/value pairs, just like a Python dictionary.
JSON is a pure string written in a convention format, which does not have any characteristics of data structure. The rules of the string representation of Python's dict look similar to JSON, but the dict itself is a complete data structure that implements all of its own algorithms.
The json. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to json. The json. dump() method allows us to convert a python object into an equivalent JSON object and store the result into a JSON file at the working directory.
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.
\<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).None
objects, JSON uses null
to as a special "doesn't exist" sentinel value.True
and False
as boolean values, JSON uses true
and false
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With