Python json to list and json to dict examples
In this post:
- Python JSON file to dictionary
- Python JSON string to dictionary
- Python JSON to list
- Python JSON to string
- Error 'module' object has no attribute 'loads'
Python JSON file to dictionary
Reading and parsing JSON files is very common operation in Python world. Python offers several different solution. Example below show simple converting of JSON file to dictionary using method from module JSON:
json.loads
Example:
import json
jsonFile = open("/home/user/sample.json")
jsonString = jsonFile.read()
jsonData = json.loads(jsonString)
type(jsonData)
print(jsonData['menu'])
result:
{u'popup': {u'menuitem': [{u'onclick': u'CreateNewDoc()', u'value': u'New'}, {u'onclick': u'OpenDoc()', u'value': u'Open'}, {u'onclick': u'CloseDoc()', u'value': u'Close'}]}, u'id': u'file', u'value': u'File'}
Python JSON string to dictionary
Sometimes the JSON is received in string format. This is a bit different from reading JSON file. Example below shows converting JSON string to a dictionary which is done by:
json.loads
Example:
import json
jsonString = """{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}"""
jsonData = json.loads(jsonString)
type(jsonData)
print(jsonData['menu'])
result:
{u'popup': {u'menuitem': [{u'onclick': u'CreateNewDoc()', u'value': u'New'}, {u'onclick': u'OpenDoc()', u'value': u'Open'}, {u'onclick': u'CloseDoc()', u'value': u'Close'}]}, u'id': u'file', u'value': u'File'}
Python JSON to list
Arrays and Lists are allowed to be used in JSON. In order to retrieve list from JSON structure you need to use access operator. So if your JSON containing objects which are Arrays / List you can access them by:
jsonlist = jsonData['list']
Example:
import json
jsonData = '{"list": ["java", "python", "groovy", "ruby"]}'
jsonData = json.loads(jsonData)
jsonlist = jsonData['list']
print (jsonlist)
result:
[u'java', u'python', u'groovy', u'ruby']
Python JSON to string
Converting JSON string can be done by two methods:
- json.dumps() - preferred way for conversion. Always produce valid JSON string
- str() - could produce invalid JSON string for some values
Example:
import json
jsonFile = open("/home/user/sample.json")
jsonString = jsonFile.read()
print(json.dumps(jsonData))
print(str(jsonData))
print(jsonData['menu'])
result:
#json.dumps(jsonData)
{"menu": {"popup": {"menuitem": [{"onclick": "CreateNewDoc()", "value": "New"}, {"onclick": "OpenDoc()", "value": "Open"}, {"onclick": "CloseDoc()", "value": "Close"}]}, "id": "file", "value": "File"}}
# str(jsonData)
{u'menu': {u'popup': {u'menuitem': [{u'onclick': u'CreateNewDoc()', u'value': u'New'}, {u'onclick': u'OpenDoc()', u'value': u'Open'}, {u'onclick': u'CloseDoc()', u'value': u'Close'}]}, u'id': u'file', u'value': u'File'}}}
Error 'module' object has no attribute 'loads'
This error:
Error 'module' object has no attribute 'loads'
can appear in your code if you name your file : json.py. And this will interfere with the import causing the error:
import json