How To Convert Python List To JSON Example
We often came across a situation where we need to convert from one data structure to another. Python has so many data structures to work with, and each structure adds something to the table. If you are working with APIs, then we need to deal with JSON data. In this tutorial, we will see How To Convert Python List To JSON Example.
Python List To JSON
Content Overview
There is an inbuilt package that python provides called json.
Import it, then make a simple list and then write json.dumps(list).
See the following code.
# app.py import json data = ["DisneyPlus", "Netflix", "Peacock"] json_string = json.dumps(data) print(json_string)
Output
➜ pyt python3 app.py ["DisneyPlus", "Netflix", "Peacock"] ➜ pyt
Python json library can parse JSON from strings or files. The library parses JSON into a Python dictionary or list. It can also convert Python dictionaries or lists into JSON strings.
Convert JSON to Python Object (Dict)
See the following code.
# app.py import json json_data = '{"name": "Krunal", "city": "Rajkot"}' python_obj = json.loads(json_data) print(python_obj["name"]) print(python_obj["city"])
Output
➜ pyt python3 app.py Krunal Rajkot ➜ pyt
Convert JSON to Python Object (List)
Let’s convert the JSON to Python Object type. See the following code.
# app.py import json array = '{"drinks": ["coffee", "tea", "water"]}' data = json.loads(array) for element in data['drinks']: print(element)
Output
➜ pyt python3 app.py coffee tea water ➜ pyt
Write Python JSON data to a file
Okay, let’s see how to write the JSON data into a file.
See the following code.
# app.py import json data = {"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"} with open('app.json', 'w') as f: json.dump(data, f)
Output
In your app.json file, you have the data json written.
utf8-encode
If we want to get the utf8-encoded, then write the following code.
# app.py import json data = {"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"} with open('app.json', 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4)
The following indented output will be written in the file.
Output
{ "Eleven": "Millie", "Mike": "Finn", "Will": "Noah" }
We can get the utf8-encoded file as opposed to ascii-encoded using the following code.
# app.py import json data = {"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"} with open('app.json', 'w', encoding='utf-8') as f: f.write(json.dumps(data, ensure_ascii=False))
Output
{"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"}
On Windows, the encoding=’utf-8′ argument to open is still necessary.
We can avoid storing an encoded copy of the data in memory (the result of dumps) and to output utf8-encoded bytestrings in both Python 2 and 3, use the following code.
import json, codecs with open('app.txt', 'wb') as f: json.dump(data, codecs.getwriter('utf-8')(f), ensure_ascii=False)
The codecs.getwriter call redundant in Python 3 but required for Python 2.
Finally, How To Convert Python List To JSON Example is over.