107
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?

2
  • 4
    What do you mean? data['fruits'] will be a list Commented Jun 11, 2012 at 1:06
  • 7
    You have a JSON Object containing an Array. A JSON Array is homologous to a Python list. A JSON Object is homologous to a Python dict. Technically, you have a dict containing a single key-value pair where the value is a list of strings. Commented Jun 11, 2012 at 1:10

4 Answers 4

167
import json

array = '{"fruits": ["apple", "banana", "orange"]}'
data  = json.loads(array)
print(data['fruits']) 
# the print displays:
# ['apple', 'banana', 'orange']

You had everything you needed. data will be a dict, and data['fruits'] will be a list

Sign up to request clarification or add additional context in comments.

4 Comments

problem is when the JSON format uses Single Quotes ' instead of double quotes "
How do you mean?
I sent a double quote " string with string qualifications using single quotes '....it could not parse the JSON with that sequence of quotations....converted to Single Quote with Double Quote qualifications and it did just fine....thought it was odd but may be a premise to parse
But where was the json in these examples ever using single quotes? Json as a format does not support single quotes. Yes you are right as a fact but I don't know where you are pointing it out here
16

Tested on Ideone.

import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data  = json.loads(array)
fruits_list = data['fruits']
print fruits_list

Comments

3

ast.literal_eval() from the Python standard library ast may be used to parse json strings as well.

It's especially useful if the string looks like a json but is actually a string representation of a Python object. In such cases, json.loads won't work but ast.literal_eval works.

import ast
array = '{"fruits": ("apple", "banana", "orange")}'
a = ast.literal_eval(array)  # {'fruits': ('apple', 'banana', 'orange')}
j = json.loads(array)        # JSONDecodeError

Comments

0

data will return you a string representation of a list, but it is actually still a string. Just check the type of data with type(data). That means if you try using indexing on this string representation of a list as such data['fruits'][0], it will return you "[" as it is the first character of data['fruits']

You can do json.loads(data['fruits']) to convert it back to a Python list so that you can interact with regular list indexing. There are 2 other ways you can convert it back to a Python list suggested here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.