Reading and writing JSON files with Python

You can use Python's json module to receive JSON-formatted files and strings as objects such as dictionaries.

1. Convert JSON string to dictionary

Use the json.loads () function.


s = r'{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'

print(s)
# {"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

d = json.loads(s)

print(d)
# {'A': {'i': 1, 'j': 2}, 'B': [{'X': 1, 'Y': 10}, {'X': 2, 'Y': 20}], 'C': 'Ah'}

print(type(d))
# <class 'dict'>

2. Read the JSON file as a dictionary

Use the json.load () function.

with open('/test.json') as f:
    print(f.read())
# {"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

3. Output the dictionary as a JSON string

Use the json.dumps () function.

d = {'A': {'i': 1, 'j': 2}, 
     'B': [{'X': 1, 'Y': 10}, 
           {'X': 2, 'Y': 20}], 
     'C': 'Ah'}

sd = json.dumps(d)

print(sd)
# {"A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}], "C": "\u3042"}

print(type(sd))
# <class 'str'>

4. Save the dictionary as a JSON file

Use the json.dump () function.


d = {'A': {'i': 1, 'j': 2}, 
     'B': [{'X': 1, 'Y': 10}, 
           {'X': 2, 'Y': 20}], 
     'C': 'Ah'}


with open('/test.json', 'w') as f:
    json.dump(d, f, indent=4)

Recommended Posts

Reading and writing JSON files with Python
Reading and writing CSV and JSON files in Python
Reading and writing fits files with Python (memo)
Reading and writing NetCDF with Python
Reading and writing CSV with Python
Reading and writing csv files
Reading .txt files with Python
Example of reading and writing CSV with Python
[Introduction for beginners] Reading and writing Python CSV files
JSON encoding and decoding with python
Python CSV file reading and writing
Reading and writing text in Python
Character code for reading and writing csv files with python ~ windows environment ver ~
[Easy Python] Reading Excel files with openpyxl
Read and write JSON files in Python
[Easy Python] Reading Excel files with pandas
uproot: Python / Numpy based library for reading and writing ROOT files
[Python] Use JSON with Python
[Python] Reading CSV files
Read JSON with Python and output as CSV
[Python3] Read and write with datetime isoformat with json
Download and import files with Splunk external python
Case sensitive when reading and writing INI files
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Sorting image files with Python (2)
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Sort huge files with python
Sorting image files with Python (3)
Sorting image files with Python
Integrate PDF files with Python
Read and write files with Slackbot ~ Bot development with Python ~
Read json file with Python, format it, and output json
python with pyenv and venv
Format json with Vim (with python)
Handle JSON files with Matlab
Reading, displaying and speeding up gifs with python [OpenCV]
Works with Python and R
Read json data with python
Easily write JSON and Python dataclass conversions with quicktype and dacite
Sample of HTTP GET and JSON parsing with python of pepper
[Python] Easy reading of serial number image files with OpenCV
Notes on reading and writing float32 TIFF images in python
Communicate with FX-5204PS with Python and PyUSB
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Recursively unzip zip files with python
Scraping with Python, Selenium and Chromedriver
Manipulate files and folders in Python
[Python] POST wav files with requests [POST]
Read and use Python files from Python
Handling of JSON files in Python
Decrypt files encrypted with OpenSSL with Python 3
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Save and retrieve files with Pepper