Working with JSON data in Python

5/5 - (1 vote)

What is JSON?

JSON stands for JavaScript Object Notation.
JSON is a text format that is completely language independent, which was discovered in 2001 and become the world’s best-loved data interchange format. Read more about JSON here.

JSON is a lightweight format for storing and transporting data, which is used when data is sent from a server to a web page.

JSON is very native to JavaScript, the literal notation for constants in JavaScript for both arrays of variable constants, values, and objects.

How JSON looks like?

Exemple of nested JSON:

products = '''
    [{
        "id" : "2657",
        "name" : "Women Sleeveless Round Neck Fit and Flare Dress",
        "price" : "47.95",
        "color" : "Black Velvet",
        "size" : {
                "L" : "2",
                "XL" : "3",
                "XXL" : "1"
                },
        "brand" : "Calvin Klein"      
    },
    {
        "id" : "1343",
        "name" : "Women Summer Dresses Soft Loose",
        "price" : "45.05",
        "color" : "Red",
        "size" : {
                "S" : "1",
                "M" : "3",
                "L" : "2",
                "XXL" : "6"
                },
        "brand" : "Zara"      
    }]'''

You can validate a JSON here. This tool check for errors and solve the problems by formatting and beautifying the JSON data so that it is easy to read and debug.

Read, Write, Parse JSON example

The example that parses an XML file, this time I will parse it using JSON.

import json
jsproducts = '''
    [{
        "id" : "2657",
        "name" : "Women Sleeveless Round Neck Fit and Flare Dress",
        "price" : "47.95",
        "color" : "Black Velvet",
        "size" : {
                "L" : "2",
                "XL" : "3",
                "XXL" : "1"
                },
        "brand" : "Calvin Klein"      
    },
    {
        "id" : "1343",
        "name" : "Women Summer Dresses Soft Loose",
        "price" : "45.05",
        "color" : "Red",
        "size" : {
                "S" : "1",
                "M" : "3",
                "L" : "2",
                "XXL" : "6"
                },
        "brand" : "Zara"      
    }]'''

products = json.loads(jsproducts)  
#products is a list of dictionaries, with key value pairs
for product in products:
    print("\nName: ",product['name'])
    print("Price: ",product['price'])    
    print("Sizes:")
    for k,v in product['size'].items():
        print("\t{}: {}".format(k,v))

//Result
Name:  Women Sleeveless Round Neck Fit and Flare Dress
Price:  47.95
Sizes:
        L: 2
        XL: 3
        XXL: 1

Name:  Women Summer Dresses Soft Loose
Price:  45.05
Sizes:
        S: 1
        M: 3
        L: 2
        XXL: 6

So you can see, it’s much simpler to parse using JSON than XML. You can use JSON instead which it’s much easier and much faster.

What is an API?

API stands for Application Programming Interface. In fact, they are ways to use web protocols to access data on systems, using well-defined approaches.
This approach improves existing methods of data sharing, by providing a service layer between systems.

Most large companies have built APIs for internal use or for their customers. The data in each application is offered up as a service that any other application can use it.

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net