Reference site: [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
}
}
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.
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:
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:
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.
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.
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