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

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

]

}

Would you like to learn more about advanced data collection techniques? Check out our next free upcoming workshops:

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 website 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.

Share:

You might also be interested in

If your company has even ONE developer dedicated to web data collection, you are wasting precious resources

The state of the economy in general, and of tech in particular, is leading many CEOs to put budget cut pressure on Information Technology execs. This article aims to help IT leaders improve their bottom lines by offering a more strategic approach to operational web data collection outsourcing

Shooting ourselves in the foot? Why we willingly killed 10% of our network

Bright Data believes in transparent and ethical practices, especially when it comes to dealing with users who make up its Residential peer network. To ensure compliance, we use advanced monitoring protocols and partner with top anti-virus companies. Sometimes, we make decisions which might seem a little crazy, like hurting our own network. That is what this post is about.
Web Data powering e-commerce

Mystery shoppers are so 2000 and late. Web data is the future of e-commerce.

We sat down with Charmagne Cruz from Shopee, the leading e-commerce platform in Southeast Asia, to discuss how the online conglomerate uses public web data to drive forward the company’s success as well as carve out a large section of the Asian e-commerce market.
Qualitative data collection methods

Qualitative data collection methods

Quantitative pertains to numbers such as competitor product fluctuations, while qualitative pertains to the ‘narrative’ such as audience social sentiment regarding a particular brand. This article explains all the key differences between the two, as well as offering tools to quickly and easily obtain target data points