![]() |
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 ListBelow, are the methods to convert Python JSON to List.
Convert Python Json To List Using JSON.loads() MethodIn 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
Output
<type 'str'> 1 <type 'list'> Convert Python Json To List Using Pandas libraryIn 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
Output : <class 'str'>
['Harsha', 'Krishna', 'Vardhan']
<class 'list'>
Convert Python Json To List Using List ComprehensionIn 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
Output
<class 'str'> ['name', 'subject', 'Code'] <class 'list'> |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |