In Python, why should one use JSON scheme instead of a dictionary?

Question:

(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?

Asked By: bordeltabernacle

||

Answers:

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).

Answered By: jfs

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

Answered By: vijay
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.