[Python] Use JSON format data as a dictionary type object

JSON format data can be used as a dictionary type object

Files with the extension .json such as xxx.json and JSON format data obtained by hitting WebAPI are It can be used as a dictionary type object on Python.

wrap up

--In Python, JSON format data can be used as a dictionary type object. --Use the json module. --Use json.load () to load the file. --Use json.loads () to read the character string. --Use json.dumps () for writing.

About JSON module

In Python, you need to ʻimport the json` module in order to use JSON format data.

jsonTest.py


import json

Try using the json module

Try reading and writing JSON-formatted data using the json module.

Create dictionary type data and save it in a JSON file

Dictionary-type objects created in Python can be saved in a JSON file by using the json module.

jsonWrite.py


import json

d = {"name":"ndj", "place":["Qiita", "Twitter", "YouTube"], "age": 25}

with open("ndj.json", mode="w") as f:
    d = json.dumps(d)
    f.write(d)

ndj.json


{
    "name": "ndj", 
    "place": ["Qiita", "Twitter", "YouTube"], 
    "age": 25
}

To save a dictionary type object in a JSON format file, use json.dumps () You need to convert a dictionary type object to a string type. Personally, I would like to note that when you do json.dumps (), all keys are forcibly converted to string type.

json.dumps () forces the key to be converted to a string

writeJson.py


import json

d = {1:"one", 100:"hundred", 1000:"thousand"}

with open("nums.json", mode="w") as f:
    d = json.dumps(d)
    f.write(d)

nums.json


{
     "1": "one", 
     "100": "hundred", 
     "1000": "thousand"
}

Read the saved JSON format data and convert it to a dictionary type object

The JSON format file saved above can be used as a dictionary type object. Of course, the same applies to JSON format data obtained by hitting WebAPI.

Use json.load () when loading a JSON file

You can get a dictionary type object by using json.load () for the opened file.

readJson.py


import json

d = {}
with open("ndj.json", mode="r") as f:
    d = json.load(f)

Use json.loads () to convert a string type object to a dictionary type

It can be converted to dictionary type as well as using json.load ().

readJson.py


import json

s = '{"name": "ndj", "place": ["Qiita", "Twitter", "YouTube"], "age": 25}'
d = {}
d = json.loads(s)

reference

Python official documentation: json

Recommended Posts

[Python] Use JSON format data as a dictionary type object
Use pymol as a python library
Use blender as a python module
Create a JSON object mapper in Python
python memo: Treat lists as a set type
[Introduction to Python] How to handle JSON format data
[Python] Use JSON with Python
Do not specify a mutable object (list type, dictionary type, etc.) as the initial value of the function argument of python.
Receive dictionary data from a Python program in AppleScript
How to write a list / dictionary type of Python3
Convert / return class object to JSON format in Python
Try writing JSON format data to object storage Cloudian/S3
Use slackbot as a relay and return from bottle to slack in json format.
Easily format JSON in Python
Use Remotte as a user
Python data type summary memo
Image data type conversion [Python]
Format json with Vim (with python)
Read json data with python
Format when passing a long string as an argument of python
[Python / Django] Create a web API that responds in JSON format
python: Use the variables defined in the string format as they are
[Python] How to store a csv file as one-dimensional array data
Use youtube_dl as a python module. appendix) Nico Nico Douga HTTP 403 error
Use DynamoDB as a lock manager
Export DB data in json format
Touch a Python object from Elixir
Python Application: Data Handling Part 3: Data Format
Python for super beginners Python # dictionary type 1 for super beginners
Dynamically load json type in python
Use embeddable Python as Vim's Python 3 interface
Launch a Python script as a service
Python for super beginners Python # dictionary type 2 for super beginners
Merge JSON format data with Ansible
Send and receive image data as JSON over the network with Python
Try to extract specific data from JSON format data in object storage Cloudian/S3
Use print in a Python2 lambda expression
Data input / output in Python (CSV, JSON)
Install Python as a Framework with pyenv
Add a Python data source with Redash
AHC task (3) Read CSV as a dictionary
A Java programmer studied Python. (About type)
Convert Excel data to JSON with python
[Python] Use string data with scikit-learn SVM
Hash in Perl is a dictionary in Python
[Python] Sorting collection types as a reference
[python] [meta] Is the type of python a type?
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
How to use "deque" for Python data
Python as a strongly, dynamically typed language
Use Django from a local Python script
[Python] I tried to get the type name as a string from the type function
A memo to generate a dynamic variable of class from dictionary data (dict) that has only standard type data in Python3