[Introduction to Python] How to parse JSON

Reference site: [Introduction to Python] How to parse JSON

[Introduction to Python] How to parse JSON

JSON is a data format used when exchanging data in a web application. JSON is a notation designed to be used by both humans and computers, but when the amount of data is large, it is difficult to read as it is, and it is necessary to parse (analyze) it to some extent. So this time, I will explain how to parse JSON in Python.

In this article, I will use a json file called "test.json" as an example.

test.json
{
    "book1":{
	"title":"Python Beginners",
 	"year": 2005 ,
	"page": 399 
},
    "book2":{
    	"title": "Python Developers",
    	"year": 2006 ,
	"page": 650 
},
"book3":{
	"title":"Python cookbook",
 	"year": 2002 ,
"page": 344 
},
"book4":{
    	"title": "Python Dictionary",
    	"year": 2012 ,
	"page": 1024 
}
}

Read JSON data

To parse JSON data in Python, you first need to read the JSON data as a dictionary. There are two main methods for reading JSON data.

Read from JSON file

This is a method to read the data saved as a JSON file. Use the load function of the json module to load the data.

import json  #Must be required
Variable 1= open(‘JSON file path to read’, ‘r’) 
Variable 2= json.load(Variable 1)  

First, the JSON file is opened with the open function in the same way as a normal file open. Then, you can read the JSON file by passing the file variable of the opened JSON file to the argument of json.load. The JSON file loaded by the load function is saved as a dictionary.

import json
 
f = open('test.json', 'r')
json_dict = json.load(f)
print('json_dict:{}'.format(type(json_dict)))

Execution result

json_dict:

Convert JSON string to dictionary type

Another way to read JSON data is to convert a JSON-formatted string to a dictionary type. Json module loads for JSON format string conversion Use a function. It's similar to the load function, so don't make a mistake.

import json
 
Variable 1= json.loads(Variable 2)  #Variable 2はJSON形式の文字列

If you pass a JSON format string as an argument, the loads function will convert it to a dictionary type and return it.

import json
 
json_str = '''
{
    "test":"json",
    "test2":"json2"
}
'''
 
json_dict = json.loads(json_str)
print('json_dict:{}'.format(type(json_dict)))

Execution result

json_dict:

Read from JSON file in order

You can load data from a JSON file by using the load function, but there is one problem with loading it as is. That's because Python dictionary types don't keep their order, so every time you read a JSON file, the order gets out of order. To prevent that, use the "object_pairs_hook" option of load.

import json
 
json_str = '''
{
    "test":"json",
    "test2":"json2"
}
'''
 
json_dict = json.loads(json_str)
print('json_dict:{}'.format(type(json_dict)))

Execution result

json_dict:

In Python, there is a type called "OrderedDict" which is a dictionary type that saves in order. By specifying "collections.OrderedDict" in the "object_pairs_hook" option when loading JSON with the load function, JSON data is loaded as the OrderedDict type, so it can be loaded in order.

Get the data you need

After reading the JSON, it's time to analyze the JSON data. When parsing JSON data, it is necessary to extract the necessary data from the JSON data, but in fact, JSON is handled as a dictionary type in Python, so it can be treated in the same way as a dictionary type.

import json
 
f = open('test.json', 'r')
json_dict = json.load(f)
 
print('Information on book1:{}'.format(json_dict['book1']))
print('Number of pages in book3:{}'.format(json_dict['book3']['page']))

Execution result

Book1 information: {‘title’: ‘Python Beginners’, ‘year’: 2005, ‘page’: 399} Number of pages in book3: 344

In this example, the value is retrieved by specifying the key like a normal dictionary type. You can also use dictionary-type methods and iterate with for statements, so you can retrieve data as you like.

import json
 
f = open('test.json', 'r')
json_dict = json.load(f)
 
for x in json_dict:
    book_page = json_dict[x]['page']
    
    if(book_page >= 500):
        print('{0}:{1}'.format(x, json_dict[x]))

Execution result

book4:{‘page': 1024, ‘year': 2012, ‘title': ‘Python Dictionary’} book2:{‘page': 650, ‘year': 2006, ‘title': ‘Python Developers’}

In this example, only the JSON data with [page] of 500 or more is extracted. In the case of Python, there are many convenient methods, so you can easily parse JSON data.

Analyze on the command line

So far, I've written Python scripts to read and parse JSON. However, it's tedious to write a script every time just to display and parse a small amount of JSON.

For those times, Python allows you to view and parse simple JSON objects on the command line. You can display the contents of the JSON file on the command line by starting the command line or terminal and entering the following.

python -m json.tool test.json

Execution result

{ “book1″: { “title”: “Python Beginners”, “year”: 2005, “page”: 399 }, “book2″: { “title”: “Python Developers”, “year”: 2006, “page”: 650 }, “book3″: { “title”: “Python cookbook”, “year”: 2002, “page”: 344 }, “book4″: { “title”: “Python Dictionary”, “year”: 2012, “page”: 1024 } }

Recommended Posts

[Introduction to Python] How to parse JSON
[Introduction to Python] How to handle JSON format data
[Introduction to Python] How to use class in Python?
How to install Python
How to install python
Introduction to Python language
How to read JSON
Introduction to OpenCV (python)-(2)
[Introduction to Udemy Python3 + Application] 23. How to use tuples
How to create a JSON file in Python
How to generate a Python object from JSON
How to make Substance Painter Python plugin (Introduction)
[2020.8 latest] How to install Python
How to install Python [Windows]
Introduction to Python Django (2) Win
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
Parse JSON file to object
How to use Python argparse
Introduction to serial communication [Python]
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
How to change Python version
An introduction to Python Programming
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
[Introduction] How to use open3d
How to use Python lambda
[Python] How to use virtualenv
Introduction to Python For, While
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Introduction to Python] How to use while statements (repetitive processing)
How to convert JSON file to CSV file with Python Pandas
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Python] How to stop the loop using break?
[Introduction to Python] How to write repetitive statements using for statements
How to handle JSON in Ruby, Python, JavaScript, PHP
How to install python using anaconda
[Introduction to Udemy Python 3 + Application] 31. Comments
How to write a Python class
[Python] How to FFT mp3 data
Python: How to use async with
Introduction to Python Numerical Library NumPy
[Introduction to Python3 Day 1] Programming and Python
[Python] How to use Pandas Series
How to collect images in Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Udemy Python 3 + Application] 56. Closure