Use the json
module loads method to read JSON files into dict in Python. JSON loads create a new dictionary with the key-value pairs of the JSON string and it returns this new dictionary.
json.loads(data_JSON)
Python read JSON files into dict example
Simple example code load JSON from a File and Parse Dumps. We have simple.json which contains json data.
import json
with open('data.json') as f_in:
res = json.load(f_in)
print(json.dumps(res, indent=3))
print(type(res))
Output:
This table presented corresponding Python values and data types.
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
json.dumps(client)
creates and returns a string with all the key-value pairs of the dictionary in JSON format.
Do comment if you have any doubts or suggestions on this Python JSON file topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.