JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used in web development and data exchange. In Python, converting JSON to a list is a common task, and there are several methods available to achieve this. In this article, we will explore four simple and commonly used methods to convert JSON to a list in Python.
Convert Python JSON to List
Below, are the methods to convert Python JSON to List.
Convert Python Json To List Using JSON.loads() Method
In this example, the below code utilizes the `json.loads()` method to convert a JSON-formatted string `[1,2,3,4]` into a Python list named `array`. The `print` statements display the type of the original string, the first element of the resulting list, and the type of the converted list, respectively.
Python
import json
#taking a string
string = "[1,2,3,4]"
#Passing the string into the json.loads() method
array = json.loads(string)
print(type(string))
print(array[0])
print(type(array))
Output<type 'str'>
1
<type 'list'>
Convert Python Json To List Using Pandas library
In this example, below code uses the `pd.read_json()` method from the pandas library to read a JSON-formatted string into a DataFrame. The subsequent `print` statements display the type of the original string, the list of names extracted from the DataFrame, and the type of the resulting list, respectively.
Python
import pandas as pd
#taking the json file .
string = '{"names":["Harsha","Krishna","Vardhan"],"subject":["Physics","Maths","Java"]}'
#using the read_json method in the pandas
dataFrame = pd.read_json(string)
#Converting into the list
print(type(string))
print(list(dataFrame["names"]))
print(type(list(dataFrame["names"])))
Output :
<class 'str'>
['Harsha', 'Krishna', 'Vardhan']
<class 'list'>
Convert Python Json To List Using List Comprehension
In this example, The code parses a JSON string containing a list of dictionaries using `json.loads()`. It then extracts the keys from the first dictionary in the list and prints the type of the original string, the extracted keys, and the type of the resulting list, respectively.
Python3
import json
#Parsing the JSON String
string = '[{"name" : "Harsha", "subject" : "Physics" , "Code" : "java" }]'
list1 = json.loads(string)
#Converting into the list
another_list = [ i for i in list1[0]]
#Extracting the keys in the dictionary
print(type(string))
print(another_list)
print(type(another_list))
Output<class 'str'>
['name', 'subject', 'Code']
<class 'list'>
Similar Reads
Convert JSON to GeoJSON Python GeoJSON has become a widely used format for representing geographic data in a JSON-like structure. If you have data in a standard JSON format and need to convert it into GeoJSON for mapping or analysis, Python provides several methods to make this conversion seamless. In this article, we will explor
4 min read
Add Json Library in Python Python JSON library is part of the Python standard library, installing it in Python doesn't require installing any other packages. It is a quick and easy process, allowing you to seamlessly work with common data formats. In this article, we will see how to add and use JSON library in Python. Add and
2 min read
Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul
3 min read
Print a List of Tuples in Python The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ].Using print()print() function is the
2 min read
Convert List Of Tuples To Json String in Python We have a list of tuples and our task is to convert the list of tuples into a JSON string in Python. In this article, we will see how we can convert a list of tuples to a JSON string in Python. Convert List Of Tuples To Json String in PythonBelow, are the methods of Convert List Of Tuples To Json St
3 min read
Convert JSON to string - Python Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data
2 min read