Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Converting List to JSON in Python (Variables and Files)

  • 18 Jan, 2025
  • Com 0
Converting Python List to JSON String

The most efficient and straightforward way to convert a Python List is by using the json.dumps() method. It accepts a list as an argument and returns a string representation of a valid JSON structure.

If you want to write JSON to a file-like object, you can use the json.dump() function.

But why do you need JSON conversion? Well, if your Python web application has to interact with web APIs, you need a standard format for data exchange, and that’s where JSON comes into play.

JSON is a lightweight, human-readable format that allows us to easily exchange data between different systems, even if they are written in other programming languages.

Understanding the process

Here is the step-by-step process that occurs under the hood when we use json.dumps() method on list object:

Step 1: Type mapping

The .dumps() function first checks the data type of each list element. Then, it maps built-in Python types to JSON equivalent types.

For example, a Python list becomes a JSON array; str becomes a JSON string; None becomes JSON null; int becomes a JSON number, etc.

Step 2: String conversion

Once the mapping is done, each element is converted into its string representation based on JSON syntax.

For example, integer 123 becomes string “123”, and the string “hello” becomes the string “\”hello\””, etc.

Step 3: Structure building

The final step is to assemble string representation into a valid JSON structure.

Process of Converting List to JSON in Python

You can see in the above figure that the data type has been changed from “list” to “str” after conversion because the JSON string is represented by str in Python.

import json

new_list = ["Disney+", 1, "Netflix", 2, "Peacock"]

print(new_list) # ['Disney+', 1, 'Netflix', 2, 'Peacock']
print(type(new_list)) # <class 'list'>

json_string = json.dumps(new_list)
print(json_string) # ["Disney+", 1, "Netflix", 2, "Peacock"]
print(type(json_string)) # <class 'str'>

In this code, you can see that we took a sample list filled with different types of values. The converted json_string is of type str, where each type is converted into JSON equivalent types.

List of Dictionaries

Let’s take an example where we will declare a list of different dictionaries and convert it into a json string.

Converting List of dictionaries into JSON

 

import json

list_of_dicts = [{'Ava': 6, 'Amy': 1}, {'Max': 5, 'Jax': 9}]

print(list_of_dicts) # [{'Ava': 6, 'Amy': 1}, {'Max': 5, 'Jax': 9}]
print(type(list_of_dicts)) # <class 'list'>

json_string = json.dumps(list_of_dicts)

print(json_string) # [{"Ava": 6, "Amy": 1}, {"Max": 5, "Jax": 9}]
print(type(json_string)) # <class 'str'>

List of Lists

List of Lists to JSON

import json

list_of_lists = [[19, 21], [11, 18], [46]]

print(list_of_lists) # [[19, 21], [11, 18], [46]]
print(type(list_of_lists)) # <class 'list'>

json_str = json.dumps(list_of_lists)

print(json_str) # "[[19, 21], [11, 18], [46]]"
print(type(json_str)) # <class 'str'>

Writing json to a file

The json.dump() method takes the list and the file object and writes to the file in JSON format.

import json

new_list = ["Disney+", 1, "Netflix", 2, "Peacock"]

with open('list_data.json', 'w') as f:
    json.dump(new_list, f)
print("The list has been written to the JSON file.")

Output

Output of Writing json to a file

 

The list has been written to the JSON file.

That’s it

Post Views: 63
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Converting a NumPy Array to Python List
Appending a New Row to an Empty Array in Numpy

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend