Python load json to list

Convert JSON to list in Python

In this tutorial, we are going to to see how to convert JSON to list in Python. JSON stands for JavaScript Object Notation which is specially formatted data used for applications.
We can convert from JSON to Python and vice versa only if they are equivalent to each other like:

  • JSON Object to a Python dictionary.
  • JSON Array to a Python list/tuple.
  • JSON String to a Python str.
  • JSON Number to a Python int/float.

Let’s check out the conversion below.

JSON exists as a string in Python. It is very common nowadays to transmit and receive data or information between a server side and web application in JSON format data.
Python already contains a built-in package json which we can use to work with JSON formatted data in Python.

All we need is to import the json as shown below to use this library:

Convert JSON array to Python list

We know that, JSON array is Python’s list. json.dumps(obj) –>Convert Python object to JSON string. json.loads(“json”) –> Convert JSON string into Python object. Hence, by using json.loads() function, one can simply convert JSON data into Python data. See the following example to know how to use it.

import json array = '' data = json.loads(array) print (data['Technology'])

Initially, we declared a JSON array, ‘array’ which is a string. json.loads(array) converts JSON array to Python object i.e. list (here) and the value is stored in data variable. Then by using the print statement, we printed out the Python list.
Note that,data will be a dict, and data [‘Technology’] will be a list.

Читайте также:  Def example in python

Источник

Python JSON to List

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.

Syntax

The syntax to use json.loads() method is

import json aList = json.dumps(jsonString)

We have to import json package to use json.dumps() function.

Note: Please note that dumps() function returns a Python List, only if the JSON string is a JSON Array.

Examples

1. Convert JSON array string to Python list

In this example, we will take a JSON Array string and convert it into Python List. The JSON Array has two elements, with each element containing two key:value pairs of each.

After loading the JSON string to list, we shall print the value for key “b”.

Python Program

import json jsonStr = '[, ]' aList = json.loads(jsonStr) print(aList[0]['b'])

2. Convert JSON array of arrays string to Python list

In this example, we will take a JSON String with Array of Arrays and convert it to Python List of Lists.

After parsing the JSON string to Python List, we shall access the first element of first first in the whole list.

Python Program

import json jsonStr = '[[], []]' aList = json.loads(jsonStr) print(aList[0][0])

Summary

In this Python JSON Tutorial, we learned how to load a JSON Array string to Python List.

Источник

Python json to list and json to dict examples

Reading and parsing JSON files is very common operation in Python world. Python offers several different solution. Example below show simple converting of JSON file to dictionary using method from module JSON:

import json jsonFile = open("/home/user/sample.json") jsonString = jsonFile.read() jsonData = json.loads(jsonString) type(jsonData) print(jsonData['menu']) 

Python JSON string to dictionary

Sometimes the JSON is received in string format. This is a bit different from reading JSON file. Example below shows converting JSON string to a dictionary which is done by:

import json jsonString = """, , ] > >>""" jsonData = json.loads(jsonString) type(jsonData) print(jsonData['menu']) 

Python JSON to list

Arrays and Lists are allowed to be used in JSON. In order to retrieve list from JSON structure you need to use access operator. So if your JSON containing objects which are Arrays / List you can access them by:

import json jsonData = '' jsonData = json.loads(jsonData) jsonlist = jsonData['list'] print (jsonlist) 
[u'java', u'python', u'groovy', u'ruby'] 

Python JSON to string

Converting JSON string can be done by two methods:

  • json.dumps() — preferred way for conversion. Always produce valid JSON string
  • str() — could produce invalid JSON string for some values
import json jsonFile = open("/home/user/sample.json") jsonString = jsonFile.read() print(json.dumps(jsonData)) print(str(jsonData)) print(jsonData['menu']) 
#json.dumps(jsonData) , , ]>, "id": "file", "value": "File">> # str(jsonData) , , ]>, u'id': u'file', u'value': u'File'>>> 

Error ‘module’ object has no attribute ‘loads’

Error ‘module’ object has no attribute ‘loads’

can appear in your code if you name your file : json.py. And this will interfere with the import causing the error:

By using SoftHints — Python, Linux, Pandas , you agree to our Cookie Policy.

Источник

Оцените статью