import json array = '{"fruits": ["apple", "banana", "orange"]}' data = json.loads(array)
That is my JSON array, but I would want to convert all the values in the fruits string to a Python list. What would be the correct way of doing this?
To convert a JSON String to Python List, use json. loads() function. loads() function takes JSON Array string as argument and returns a Python List.
To convert a list to json in Python, use the json. dumps() method. The json. dumps() is a built-in function that takes a list as an argument and returns the json value.
Use json. dumps() to convert a list to a JSON array in Python. The dumps() function takes a list as an argument and returns a JSON String.
import json array = '{"fruits": ["apple", "banana", "orange"]}' data = json.loads(array) print data['fruits'] # the print displays: # [u'apple', u'banana', u'orange']
You had everything you needed. data
will be a dict, and data['fruits']
will be a list
Tested on Ideone.
import json array = '{"fruits": ["apple", "banana", "orange"]}' data = json.loads(array) fruits_list = data['fruits'] print fruits_list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With