39

(This is an edit of the original question) "What's the difference between Python Dictionary and JSON?" "needed clarity" although for me it is very clear and logical! Anyway, here's an attempt to "improve" it ...


Is there any benefit in converting a Python dictionary to JSON, except for transferability between different platforms and languages? Look at the following example:

d = {'a':111, 'b':222, 'c':333}
print('Dictionary:', d)
j = json.dumps(d, indent=4)
print('JSON:\n%s' % j)

Output:

Dictionary: {'a': 111, 'b': 222, 'c': 333}
JSON:
{
    "a": 111,
    "b": 222,
    "c": 333
}

They are almost identical. So, why should one use JSON?

RK7
  • 7
  • 3
bordeltabernacle
  • 1,603
  • 5
  • 24
  • 46
  • 1
    If you have a python dict that you are using in python, it seems like unnecessary work to convert it to another language's format if you don't need to. – khelwood Oct 16 '15 at 11:34
  • Do you mean you want to store JSON in some files, or just pass dicts around as JSON within one program? There’s no reason to do the latter. – Ry- Oct 16 '15 at 11:35
  • 17
    JSON is data exchange format. You convert to it when you need to make the data available externally. Using it internally makes about as much sense as storing numbers in strings. –  Oct 16 '15 at 11:35
  • 1
    Yeah, this is what I thought, it seemed like an unnecessary step to do within a single program. I guess I wasn't sure at what point you would then choose to convert the data to JSON, but understanding it as an *exchange format* makes that clearer. Thanks. – bordeltabernacle Oct 16 '15 at 11:51
  • 1
    What J.F. Sebastian says. So you _might_ want to use JSON to store that dict to disk so you can read it back on a subsequent run of the program, and so other programs can read that data. However, you can use the Python pickle module to persist data like that, and pickle can handle things that JSON can't, OTOH, pickle is Python-specific & not human-readable. – PM 2Ring Oct 16 '15 at 12:02

2 Answers2

81

It is apples vs. oranges comparison: JSON is a data format (a string), Python dictionary is a data structure (in-memory object).

If you need to exchange data between different (perhaps even non-Python) processes then you could use JSON format to serialize your Python dictionary.

The text representation of a dictionary looks like (but it is not) json format:

>>> print(dict(zip('abc', range(3))))
{'a': 0, 'b': 1, 'c': 2}

Text representation (a string) of an object is not the object itself (even string objects and their text representations are different things e.g., "\n" is a single newline character but obviously its text representation is several characters).

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 3
    *representation of* vs. *the object itself*, this is what confused me, how JSON is different from a python nested dict. This makes sense now, thankyou! – bordeltabernacle Oct 16 '15 at 11:54
  • 1
    @RMPhoenix: to make sure that you understand the difference, try to figure out what is `len(json.loads('"\\\\"'))`? (if you can't get the answer at once; write down the solution step by step). – jfs Oct 16 '15 at 12:06
  • Challenge accepted. OK, I don't have much experience with JSON, so I don't fully understand the answer to this. This is converting JSON to Python. The answer is `1`, as this boils down to a single `\`. The `\` in front of it escapes it. The `"\\"` seems to be json specific, something to do with hex? So while as a text representation there are 6 characters, it ultimately evaluates to just the 1, the others are like context managers I guess, encoding/decoding signals signifying coded meaning. – bordeltabernacle Oct 16 '15 at 13:19
  • 1
    @RMPhoenix: the key word here is "double escaping": the backslash is special inside *both* Python string literals *and* JSON text: it may change how the following character is interpreted e.g., `'"\t"'` in Python source code is a tab (single character) surrounded by double quotes but `'"\\t"'` is two characters (backslash + `t`) surrounded by quotes but backslash is special in JSON too and therefore `json.loads('"\\t"') == '\t'` i.e., we start with `\\t` in Python source (literal) then the json parser sees `\t` (two characters) that finally results in a *single* tab character. – jfs Oct 16 '15 at 13:40
  • 1
    Why [mongodb](https://docs.mongodb.com/manual/introduction/) says, *JSON objects*? but not JSON format? – overexchange Jul 24 '17 at 21:56
  • @overexchange why shouldn't it say "JSON object"? I don't see how it is related to the current question. – jfs Jul 25 '17 at 01:00
  • @J.F.Sebastian Do u mean, as mongoDB stores document with json syntax? So, we can call it it json object. – overexchange Jul 25 '17 at 02:22
  • @overexchange does it say that it uses json syntax? It is unrelated – jfs Jul 25 '17 at 02:33
-25

Yes there is benefit, With json you can play with data and make it look simple, instead of unnecessarily complicating data with '[' brackets

well you are right python dictionary is not pleasant for the eyes

Also I am using this json wrapper instead of dictionary in my python project

Also if you are worried about performance dictionary vs json then there is no difference.You can use json

So my code looks clean with json

Manish
  • 1
  • 2
vijay
  • 10,276
  • 11
  • 64
  • 79
  • 2
    This is so far the most non-pythonic coding practice in Python I've read. – Manu mathew Jul 24 '20 at 13:21
  • 5
    this answer is just factually incorrect. json supports arrays via brackets. There is a difference between json and python dict. it also uses unhelpful language like "sucks" and makes subjective statements like "makes code look dirty" without backing up the claim. – ex-zac-tly Aug 27 '20 at 15:02
  • Yeah, this answer is conflating JSON (a format) with the way property access works in Javascript. If anyone wants the ability to access keys as attributes, though, the https://pypi.org/project/attrdict/ package looks like it provides this. – zmccord Dec 10 '21 at 23:32