Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON array to Python list

Tags:

python

json

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?

like image 673
user1447941 Avatar asked Jun 11 '12 01:06

user1447941


People also ask

How do I read a JSON file and convert to list in Python?

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.

How do I list a JSON in Python?

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.

How do you Jsonify an array in Python?

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.


2 Answers

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

like image 102
jdi Avatar answered Oct 05 '22 11:10

jdi


Tested on Ideone.

 import json array = '{"fruits": ["apple", "banana", "orange"]}' data  = json.loads(array) fruits_list = data['fruits'] print fruits_list 
like image 39
Sagar Hatekar Avatar answered Oct 05 '22 10:10

Sagar Hatekar