DEV Community

Cover image for Parsing JSON data with Python
Dvir_BD
Dvir_BD

Posted on

Parsing JSON data with Python

Defining JSON
JSON, or JavaScript Object Notation is a format commonly used to transfer data (mainly by APIs) in a way that will not be ‘heavy on the system’. The basic principle is utilizing text in order to record, and transfer data points to a third party.

The rules of JSON Syntax
JSON’s syntax is identical to JavaScript (JS) as JSON is essentially an offshoot of JS. Here are the major rules:

One: ‘Arrays’ are displayed in square brackets

Example:

“companies”:[
    {“BrandName”:”Adidas”, “NumberofEmployees”:”20,000″},
    {“BrandName”:”Nike”, “NumberofEmployees”:”31,000″},
    {“BrandName”:”Asics”, “NumberofEmployees”:”14,000″}
]
Enter fullscreen mode Exit fullscreen mode

Two: ‘Objects’ are flanked by curly brackets

Example: {"BrandName":"Adidas", "NumberofEmployees":"20,000"}
Enter fullscreen mode Exit fullscreen mode

Three: Data points are separated by commas

Example: "Asics", "Adidas", "Nike"
Enter fullscreen mode Exit fullscreen mode

Four: Data points appear in pairs of ‘keys’ and ‘values’

Example: "BrandName":"Adidas"
Enter fullscreen mode Exit fullscreen mode

Here is the subtotal of all the above parts when combined to display a JSON array of three company records (objects), and the number of employees currently employed at each respective corporation:

{
“companies”:[
{“BrandName”:”Adidas”, “NumberofEmployees”:”20,000″},
{“BrandName”:”Nike”, “NumberofEmployees”:”31,000″},
{“BrandName”:”Asics”, “NumberofEmployees”:”14,000″}
]
Enter fullscreen mode Exit fullscreen mode

}

JSON in the context of Python
The good news is that Python supports JSON natively. When looking to use JSON in the context of Python, one can enjoy the ease of using Python’s built-in package: ‘The JSON encoder and decoder.’ Give this documentation a good read, and it will be instrumental in helping you kickstart your JSON/Python conversion. To get you started, the first string of code you will need in order to import JSON to Python is:

>>> import json
Here is an example of the structure of what will typically follow:

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
Enter fullscreen mode Exit fullscreen mode

Keep in mind that JSON information is usually stored in ‘string variables,’ as is the case with the vast majority of APIs. These ‘string variables’ need to be parsed into the Python dictionary (see the next section) before any further actions can be completed in the target language (Python). As demonstrated in the example code snippet above firstly, you want to import the Python JSON module, which contains the load and loads (note that the ‘s’ here stands for ‘string’) functions.

A useful tool: JSON -> Python ‘dictionary’
As with any language, different ‘items’ are said/written differently yet mean the same thing. This is the concept of a dictionary. ‘Chair’ in English is ‘Chaise’ in French. Here is your ultimate JSON -> Python dictionary of the most common/useful terms:

Data collection automation: JSON/Python alternatives
Bright Data’s Data Collector gives busy professionals a way to collect large amounts of web data without having to write any code. Many companies trying to collect data for competitive intelligence, dynamic pricing strategies, or user-driven market research are actually targeting many of the same websites. That is why Bright Data created different web scrapers, including hundreds of ready-to-use, site-specific web crawlers.

Top comments (0)