Json with Python


Json with Python

JSON stands for JavaScript Object Notation, a popular data interchange standard on the internet. JSON is the best format for storing and structuring data between clients and servers, and it has a syntax comparable to that of JavaScript. JSON's primary goal is to transfer data between the client and the web server. It's simple to understand and the most efficient technique to transfer data. It works with various programming languages, including Python, Perl, Java, and others.

JSON mainly supports 6 types of data type In JavaScript:

  • String\sNumber\sBoolean\sNull\sObject\sArray
  • JSON is built on the two structures:

    • Name/value pairs are used to store data. It's a keyed list, an object, a record, a dictionary, and a hash table.
    • An array, vector, list, or sequence is used to represent an ordered list of values..
    { "book": [ { "id": 01, "language": "English", "edition": "Second", "author": "Derrick Mwiti" ], { { "id": 02, "language": "French", "edition": "Third", "author": "Vladimir" } }

    Working with Python JSON

    The json module is available in Python. The JSON API works the same way as the standard library marshal and pickle modules in Python, and python has built-in support for JSON features.

    Serialization is the process of encoding JSON data. Serialization is a network transmission technique that transforms data into a series of bytes.

    Deserialization is the process of decoding data that has been transformed to the JSON format in reverse.

    • This module has a lot of built-in features.
    • Take a look at the following functions:
    • print(dir(json)) import json

    Output:

    [ 'JSONDecodeError', 'JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_encoder', 'codecs', 'decoder', 'detect_encoding', 'dump', 'dumps', 'encoder', 'load', 'loads','scanner' ]

    In this section, we will learn the following methods:

    • load()
    • loads()
    • dump()
    • dumps()

    Serializing JSON

    The method of converting Python objects to JSON is known as serialization. When computers need to process a large amount of data, it's good to save that data in a file. The JSON function can be used to save JSON data to a file. The dump() and dumps() methods of the json module are used to modify Python objects.

    The following JSON objects are created from Python objects. The following is a list of the items:

    Python Objects JSON
    Dict Object
    list, tuple Array
    Str String
    int, float Number
    True true
    False false
    None null

    The dump() function

    Writing JSON Data into File

    The dump() function in Python allows you to provide data in JSON format. It takes two positional parameters, the first of which is the data object to be serialised, and the second of which is the file-like object to which the bytes should be written.

    Consider the following example of serialisation:

    Import json # Key:value mapping student = { "Name" : "Peter", "Roll_no" : "0090014", "Grade" : "A", "Age": 20, "Subject": ["Computer Graphics", "Discrete Mathematics", "Data Structure"] } with open("data.json","w") as write_file: json.dump(student,write_file)