How to parse JSON data with Python

Here is your ultimate ‘quick, and dirty’ guide to JSON syntax, as well as a step-by-step walkthrough on ‘>>> importing json’ to Python, complete with a useful JSON -> Python dictionary of the most commonly used terms, making your life that much easier
How to parse JSON data with Python
Gal El Al of Bright Data
Gal El Al | Director of Support
09-May-2022

In this article, we will cover:

Defining JSON

JSON, or Java Script 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″}

]

Two: ‘Objects’ are flanked by curly brackets 

Example: {"BrandName":"Adidas", "NumberofEmployees":"20,000"}

Three: Data points are separated by commas

Example: "Asics", "Adidas", "Nike"

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

Example: "BrandName":"Adidas"

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″}

]

}

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 extremely useful 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"])


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 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:

JSONPython 
Object Dict 
Array List 
Array Tuple 
String Str 
Number Int 
Number Float 
True True 
False False 
Null None 

Data collection automation: JSON/Python alternatives

While parsing JSON data with Python might be fun for some, it can be a terrible, time-consuming chore for others. This is where data collection tools come into play. While it is always nice to know a new language, it does not mean that you necessarily want to bog down your workflow by exclusively communicating in ‘French’. 

Bright Data’s Data Collector offers busy professionals a zero-code alternative to collecting web data at scale. 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 created different web scrapers that include hundreds of ready-to-use, site-specific web crawlers.

Ready to take the plunge? 

Gal El Al of Bright Data
Gal El Al | Director of Support

Head of Support at Bright Data with a demonstrated history of working in the computer and network security industry. Specializing in billing processes, technical support, quality assurance, account management, as well as helping customers streamline their data collection efforts while simultaneously improving cost efficiency.