Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between str(dict) and json.dumps(dict)

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
like image 799
hartmut Avatar asked Dec 05 '17 08:12

hartmut


People also ask

What is the difference between JSON dump and JSON dumps?

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.

What is a JSON dict?

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.

What is the difference between dict and JSON?

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.

What is a JSON dumps?

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.


1 Answers

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 uses null to as a special "doesn't exist" sentinel value.
  • Python uses True and False as boolean values, JSON uses true and false.
  • 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.

like image 96
Martijn Pieters Avatar answered Oct 18 '22 08:10

Martijn Pieters