How to Convert an Array in a JSON String to a List in Python
To convert an array in a JSON string into a list in Python, use the loads()
method from the json
package and access the array by its key name.
import json
items = '{"fruits": ["pear", "apple", "peach"]}'
json_obj = json.loads(items)
converted_items = json_obj["fruits"]
print(converted_items)
['pear', 'apple', 'peach']
You will need to import the json
package if you have not already done so elsewhere in your program. If something other than a string is supplied to json.loads()
a TypeError
will be thrown.
json
string