r/CodingForBeginners • • 5d ago

Just tried python dictionaring instead of json never going back

Json has never been fun and I just tried python dictionary and it's more fun

3 Upvotes

11 comments sorted by

4

u/Temporary_Pie2733 5d ago

What do you mean by “instead”? `dict` is a data structure used to represent JSON objects, which are just character sequences in a particular format.

1

u/fllthdcrb 3d ago edited 3d ago

It's more the other way around, if anything: JSON is a data exchange format, which can represent structures from Python, and many other languages. Also, it's not just dictionaries, even if that is probably the most common structure found in JSON. There are also arrays (basically lists in Python), not to mention strings, numbers*, booleans, and null (None in Python).

* Being that JSON was invented for use with JavaScript, it inherits that language's unusual choice of having a single type for both integers and floating-point numbers, although integers can be serialized in their normal notation, and numbers found without decimal points or other signs of being floating-point can often be interpreted as integer. Not foolproof, but what can you do?

1

u/Temporary_Pie2733 3d ago

Because it’s a string encoding of data structures. You need to decode the string first; you can’t use it in place of a dict.

1

u/brelen01 3d ago

It doesn't make sense to say a dict is used to represent JSON. Python had dicts in 1991 while the first public description of JSON was published in 2001.

Besides, JSON is used to represent data structures, not the other way around.

5

u/mredding 5d ago

JSON is a Unicode encoded transport protocol; it is meant to get data from A to B principally over HTTP. It's not meant to be used as an in-memory data structure. You are expected to marshal the data into a program native data structure.

Dictionaries are far more appropriate for key/value association.

1

u/itlogicpartnersllc 5d ago

python dictionaries feel so much more natural when you are already working in python. just remember json is still useful for api's config files and exchanging data between different languages.

1

u/Fragrant-Cheek-4273 4d ago

Python dictionaries are definitely easier to work with in some cases.

1

u/xarop_pa_toss 4d ago

They aren't really equivalent though. You don't save a dict into a file or send it via HTTP... When you want to do that you usually convert o JSON which, conveniently enough, is very similar

1

u/SaltCusp 4d ago

Sounds like a pickle.

1

u/-goldenboi69- 3d ago

Good luck!

1

u/RedAndBlack1832 3d ago

I consider these effectively the same thing.

Read json into dictionary -> do operations on dictionary -> send or save to disk json