pythonjsonlisttypestype-conversion

Convert JSON array to Python list


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?


Solution

  • 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