Link Search Menu Expand Document

JSON Data and Python

What is JSON?

JSON (JavaScript Object Notation) is a data-interchange notation, easy for humans to read and write. It is close to humans as well as easily understandable for computers to parse and generate. JavaScript programming language is the base of JSON. JSON is a text format that is language independent and usable in Python, Perl among other languages. Its primary use is to communicate data between a server and web applications. There are two structures of JSON:

  • A group of name/value pairs, referring to an object, record, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. Acts like an array, vector, list, or sequence.

Python and JSON

Python has a built-in library that supports JSON file and helps in encoding and decoding of data.

Import json

Serialization is the process of encoding JSON Data in any coding language. Serialization refers to transforming data into a series of bytes to be stored across a network. Similarly, deserialization is the process of decoding data that has been stored or distributed in the JSON standard.

Serialization

The process to convert JSON Data into Python is by using a built-in library function JSON.dump (). Here is the comparison table to data types, which helps translate data into Python.

PythonJSON
DictObject
List, tupleArray
Int, long, floatNumber
StrString
True, FalseTrue, false
Nonenull

 

Here is an example with python code using JSON Data and library.

data = {
    "president": {
        "name": "Donald Trump",
        "country": "United States of America"
    }

It is crucial to save this information to disk, so the user’s mission is to write it to a file.

Using Python's context manager, the user may create a file called data_file.json and open it in writeable mode. (JSON files conveniently end in a .JSON extension.)

with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)

In the above example, the file-like object is absent since it is not writing to the disk.

Keyword Arguments

Indent Keyword

The first option user wants to make amendments in is whitespace. Users may use the indent keyword to identify the indentation size for nested formats. Refer to the example below:

 

 

Separator Keyword

Separator keyword argument is another formatting option. It is a 2-tuple of the separator strings (", ", ": ") by default, but a mutual alternative for compact JSON is (",", ":"). 

>>> json.dumps(data)

>>> json.dumps(data, indent=4)

De-serializing Python

Just like serialization, there is a conversion table for deserialization, which is vice-versa.

JSONPython
ObjectDict
ArrayList
StringStr
Number (int)Int
Number (real)Float
True / FalseTrue / False
nullNone

 

The concept is more like teleportation, as it refers to that if the user encodes an object now and later on, decode it, the efficiency of the result may not be 100%.

JSON.dump () vs JSON.dumps ()

JSON.dump() functions as when JSON needs to be dump into a file.

JSON.dumps() is used to dump strings for printing and parsing.

 

Real-World Example

Here is a complete example of Python with JSON using Keywords.

import json
person_string = '{"name": "Canel", "languages": "French", "number": [6, 32, null]}'
#importing dictionary
person_dict = json.loads(person_string)

# Keywords
print(json.dumps(person_dict, indent = 4, sort_keys=True))

Output

{  "languages": "French",
    "name": "Canel",
    "numbers": [
        6,
        32,
        Null ]}

Keys are in ascending order. Indentation is 4 however, the default value if indent is none. The default value of sort_key is false.

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy