Reference site: [Introduction to Python] How to handle JSON format data
When exchanging data with a web application, data may be exchanged in the format of "JSON". JSON is used in various languages such as C and JAVA, and of course Python can handle it as well. This time, I'll cover the basics of how to work with JSON in Python.
table of contents 1 [What is JSON](## What is JSON) 2 [Handle JSON](## Handle JSON) 2.1 [Read JSON file](## Read JSON file) 2.2 [Convert JSON](## Convert JSON) 2.3 [Write JSON](## Write JSON)
First of all, I will explain what kind of format JSON is. JSON is an abbreviation for "JavaScript Object Notation" and can be said to be a "data format based on the notation of the JavaScript language". However, the notation is just JavaScript-based and can be used in many other languages. In JSON, a number and a key pair that is the name of that number are paired with a colon, separated by commas, and enclosed in curly braces.
{
"book1":{
"title": "Python Beginners",
"year": 2005 ,
"page": 399
},
"book2":{
"title": "Python Developers",
"year": 2006 ,
"page": 650
}
}
Now let's see how to actually handle JSON-formatted data in Python.
To work with a JSON file, you first need to read the data from the JSON file. The procedure to read the JSON file is (1) Open the JSON file (2) Read the opened file as JSON There are two steps. Specifically, it is described as follows.
import json #Must be required
Variable 1= open(‘The path of the JSON file to read’, ‘r’) #Here is(1)
Variable 2= json.load(Variable 1) #Here is(2)
In order to use JSON related functions, you first need to import the json module. Don't forget. Then open the JSON file as a read file. This is the same as a normal file operation. Then save the loaded JSON file as a JSON object using the load function. You have now loaded the JSON.
When you load a JSON file with the load function, it is saved as a dictionary for ease of use in Python. The dictionary type is convenient because you can easily retrieve elements, but sometimes you want to treat it as a JSON-formatted character string. In that case, let's convert from dictionary type to JSON format string. Use the dumps function to convert a dictionary to a JSON-formatted string.
import json
Variable 1= json.dumps(Variable 2) #Variable 2 は辞書型
dumps is also a function of the json module, so import the json module before using it.
dumps is a function that takes a dictionary type as an argument, converts it to a string and returns it.
import json
f = open('test.json', 'r')
json_dict = json.load(f)
print('json_dict:{}'.format(type(json_dict)))
print('-----Convert from dictionary type to JSON format string-----')
json_str = json.dumps(json_dict)
print('json_str:{}'.format(type(json_str)))
Execution result
json_dict: —– Convert from dictionary type to JSON format string —– json_str:
On the contrary, you can also convert a JSON-formatted string to a dictionary type. Use the loads function of the json module for conversion.
import json
-Since it is the same as the above example, it is omitted.
json_dict2 = json.loads(json_str)
print(‘json_dict2:{}’.format(type(json_dict2)))
Execution result
json_dict: —– Convert from dictionary type to JSON format string —– json_str: —– Convert JSON format string to dictionary type —– json_dict2: Write JSON
JSON data handled by Python can be written to a file. Writing to a file Only use the dump function of the json module. Please note that it is not dumps.
import json
Variable 1= open(‘Writing file path’, ‘w’) #Open the file to write
json.dump(Variable 2,Variable 1) #Variable 2 は辞書型
dump takes the dictionary type variable you want to write and the file to write to as arguments. You can write a JSON-formatted string with the write function like a normal file write, but if you want to write a dictionary type, this method does not need to be converted one by one, so it is easy. So, as a summary, let's see the flow of the list that reads the following json file, converts it to a character string, makes it dictionary type again, and writes it to another json file.
test.json
{
"book1":{
"title":"Python Beginners",
"year": 2005 ,
"page": 399
},
"book2":{
"title": "Python Developers",
"year": 2006 ,
"page": 650
}
}
import json
#Read JSON file
f = open('test.json', 'r')
json_dict = json.load(f)
print('json_dict:{}'.format(type(json_dict)))
#JSON data conversion
print('-----Convert from dictionary type to JSON format string-----')
json_str = json.dumps(json_dict)
print('json_str:{}'.format(type(json_str)))
print('-----Convert JSON format string to dictionary type-----')
json_dict2 = json.loads(json_str)
print('json_dict2:{}'.format(type(json_dict2)))
#Writing JSON data
f2 = open('test2.json', 'w')
json.dump(json_dict2, f2)
Execution result
json_dict: —– Convert from dictionary type to JSON format string —– json_str: —– Convert JSON format string to dictionary type —– json_dict2: test2.json {“book2″: {“year”: 2006, “title”: “Python Developers”, “page”: 650}, “book1″: {“year”: 2005, “title”: “Python Beginners”, “page”: 399}}
Recommended Posts