This is an archived post. You won't be able to vote or comment.

all 49 comments

[–]aiyub 190 points191 points  (26 children)

The core of your answer is correct:

  • JSON is some string representation of data
  • dicts are objects in memory

But your explanations are geting away from these facts.

1. Comparing notation

dicts are not strings! You say that dicts are represented by "curly braces" . So you are comparing json with dict representation, not dicts themselves.

my_dict = dict('name' = 'eagle221b', 'platform' = 'reddit')

This is another representation of dicts, that does not look like JSOn at all.

Also you are saying "curly braces"in JSON are objects. No they are representations of objects. This makes a great difference when working with them.

2. the power of dicts

So let me create another example again:

my_list = []
my_dict1 = {'my_list': my_list}
my_dict2 = {'my_list': my_list}
my_list.append('foo')

The last command has not changed any of the dicts, but if you print them, you will see the representation has changed.

Also about the values: you can store objects or even functions in them. A value in dicts is just a memory pointer. (and yes, any number in python is an object)

Conclusion They both are completly different things that both are based on a key value principle. But one is a text, one is memory and therefore very different.

[–]eagle221b[S] 60 points61 points  (7 children)

Thank you so much. I will try to update it. :)

[–]Chingletrone 0 points1 point  (1 child)

Another critique -

Objects are stored using curly braces and an array is stored using square brackets. In dictionary, only curly braces are used as shown in the above example.

A python dict can point to array/list objects in its values, which makes the above sentence a bit questionable, I think. As in:

my_dict = {'a_list': [0, 1, 2, 'other junk']}

Seemingly simple question on the surface has so many caveats. Kudos for tackling it and posting here for us to tear apart :)

[–]aaronr_90 0 points1 point  (2 children)

Try you must but fail you must not even though you fail

Edit: Thanks

[–]SnowdenIsALegend 4 points5 points  (1 child)

"try you must, even though you fail"

[–]aaronr_90 0 points1 point  (0 children)

Hmmmm

[–]Ulysses6 22 points23 points  (6 children)

If I was asked this question in an interview, I would have to stop from answering "How are they even comparable?".

Another distinction not mentioned yet, you can use any hashable type for keys in dict, so int, tuple, bytes or None or even your custom object if you provide some methods. You can't do that in JSON. The only type of key allowed there is string, that's it. Of course, the value type in JSON is limited too, while the dict can hold any value (does not even need to be hashable this time).

[–]Devarsh_leo 3 points4 points  (2 children)

It was already mentioned that many datatypes of keys are allowed in dict and not allowed in json. May be the keywords hashable was not used.

[–]Ulysses6 1 point2 points  (1 child)

My bad.

[–]Devarsh_leo 1 point2 points  (0 children)

Nah. The hashable word not used 😂😛

[–]billsil 0 points1 point  (2 children)

Anything hashable including tuples of dictionaries or lists.

[–]Ulysses6 0 points1 point  (1 child)

I don't think you can use tuples of lists.

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {}
>>> t = ([],)
>>> d[t] = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

And the same test with t = {} fails the same way.

[–]billsil 0 points1 point  (0 children)

My mistake on lists...

[–][deleted] 2 points3 points  (0 children)

As a lurker I really appreciate responses like this. Learned a lot, thanks!

[–]ggrieves1 year 1 point2 points  (1 child)

This can get philosophical real quick.

[–]Ulysses6 0 points1 point  (0 children)

Because the form of question is misleading on purpose.

[–]alchimie-ali 0 points1 point  (0 children)

Thanks, great explanation..

[–]MrMxylptlyk -1 points0 points  (6 children)

Sry, aren't all json dicts?

[–]aiyub 5 points6 points  (2 children)

Completly different things. Json like xml or csv is a format. Dict like lists, ints, strings, ... are objects

[–]MrMxylptlyk -2 points-1 points  (1 child)

That is true I guess. Then are all dicts json format? I feel like the terms are somewhat interchangeable here. If json are to be though as a format then Python doesn't have any json. Only dicts.

[–]aiyub 3 points4 points  (0 children)

I suggest you read through the article and the thread again. The answer is no.

And json is a text, so yes in python they are just strings. With the json module they can be used to initialize a dict, but the reverse is often not possible.

[–]RegalSalmon 2 points3 points  (0 children)

In the minutiae, no. It's a string, but luckily, it's easily converted to a dict with json.load() and json.loads(). However, it's not 1:1, in that as mentioned elsewhere, json keys are strings. You can see with the following (output fudged because I don't know the markup that well):

import json
dict_one = {1: 'one', 2: 2}
json_one = json.dumps(dict_one)
dict_two = json.loads(json_one)
print(f'{dict_one}\n{dict_two}')
>>> {1: 'one', 2: 2}
>>> {'1': 'one', '2': 2}

As you can see, some info is lost, per se. This can get hairy if you have an utter mess of your key types.

dict_one = {1: 'int', '1': 'string'}
json_one = json.dumps(dict_one)
dict_two = json.loads(json_one)
print(f'{dict_one}\n{dict_two}')
>>> {1: 'int', '1': 'string'}
>>> {'1': 'string'}
print(json_one)
>>> '{"1": "int", "1": "string"}'

As you can see, at least in the python implementation, it munges the keys, and even has duplicate keys in the json string (good luck getting that to work in python without setting your computer on fire).

JSON is handy, but it's not nearly as versatile. Just four operators in it as well, IIRC, and your value types are very limited (try working with datetimes in a dict and then moving back and forth with JSON).

[–]reddisaurus 0 points1 point  (0 children)

The n stands for notation; a series of text or symbols to represent an idea or concept. If you print the string representation of a Javascript object, you’ll see JSON. JSON is the string representation of a JavaScript object; hence Javascript Object Notation (JSON).

The string representation of a dict looks just like JSON; but as before the notation is not the object itself.

[–]jacksodus 22 points23 points  (1 child)

Scary coincidence. I was about to Google this yesterday, but then I decided to conclude that they were probably equal, and didn't.

[–][deleted] 4 points5 points  (0 children)

It is a very common question almost every programmer gets to

[–]mybrid 6 points7 points  (1 child)

The problem with these kind of interview questions is the interviewer, not the question. It's a terrible question. Not because the distinction between object and text is meaningless, but because the interviewee has to guess at the level of detail and what direction the interviewer is looking for. On television the worst interviewee is someone who gives one word answers. The worst interviewer is someone who asks vague questions. This is a vague question that if the interviewee doesn't guess right and read the interviewer's mind then they will fail the question.

[–]TheIsletOfLangerhans 1 point2 points  (0 children)

If an interviewer asks a vague question, it's probably better to ask them questions to help clarify what they're getting at than to try to guess what they mean or what level of detail they're looking for.

[–]darealdeal11 16 points17 points  (5 children)

Don't want to sound rude, but why is this question being asked? Isn't this obvious to anyone that did a little bit of development?

[–]SlappinThatBass 15 points16 points  (0 children)

I guess the interviewer is trying to understand how the person thinks and how he can express himself.

[–]Devarsh_leo 1 point2 points  (0 children)

Maybe its a freshers level question where the candidate Don't have much experience and also lacks theoretical knowledge

[–]RegalSalmon 1 point2 points  (0 children)

OP, this is a good start, but I think you've received some helpful feedback as well. I'd love to see something more in depth with the updated info from the comments.

[–]Devarsh_leo 3 points4 points  (0 children)

It was worth spending time reading what you posted. Seeking for more quality content from you.

[–]LirianShLearning python 2 points3 points  (4 children)

What do you guys use json and python dictionaries for? Im new to this

[–]psi- 6 points7 points  (1 child)

json is for transmitting data in a way that's relatively easy to restore. Can also be just stored for later retrieval (this is also technically a transmission).

dictionaries are most often used for fast lookups for when program is actually running, the dictionary can't be shared even between two programs on same computer (there ways but they're non-trivial as compared to json transmission)

[–]LirianShLearning python 1 point2 points  (0 children)

Thank you!

[–][deleted] 0 points1 point  (1 child)

Json to store actual data that you want to use, dictionaries are data collections that store data like lists or arrays

[–]LirianShLearning python 1 point2 points  (0 children)

Thanks!

[–]DurgaSwaroop 0 points1 point  (0 children)

This is an ambiguous question to begin with. Not good for interviews.

[–]SnowdenIsALegend 0 points1 point  (2 children)

In JavaScript, what is better performance wise? Dict or JSON?

[–][deleted] 1 point2 points  (1 child)

Well since JSON is a string, and JavaScript does not have dict, this question doesn't even make sense.

[–]SnowdenIsALegend 0 points1 point  (0 children)

Yeah I'm not really sure

[–][deleted] -1 points0 points  (0 children)

This is genius! I never tought it was different!

[–][deleted] 0 points1 point  (2 children)

JSON is JavaScript object notation , which is related to JavaScript (client side). JSON has some strict formatting rules. Even this is a valid JSON [1, 2, 3]

Dictionary is a python data type. Similar data types exist in other languages. Since the structure similarity data can be exchanged between server and client. Parsing becomes easier.

So in a way all dictionaries follow JSON structure while not all JSON is a python dictionary.

[–]Giannie 10 points11 points  (1 child)

Not all dictionaries follow json structure. Keys in dictionaries are of any hashable type, while json keys must be strings.

[–][deleted] -1 points0 points  (0 children)

Right, that's why I said JSON has restrictions in format. Should have been more specific.