DEV Community

Cover image for Convert Python Dict to JSON: Full Tutorial For Beginner
Sona
Sona

Posted on

Convert Python Dict to JSON: Full Tutorial For Beginner

When you’re coding in Python, you often deal with JSON, its a format that is used to store and exchange data. If you’ve worked with APIs, you’ve likely parsed JSON responses before.

JSON is like a dictionary in Python, storing data as key-value pairs, making it easy to understand and work with. Python’s json module helps convert Python dictionaries to JSON strings and vice versa.

In this tutorial, our main focus is on converting python dictionaries to JSON using the json module. Let’s dive into the code!

Now, to convert a Python dictionary to JSON string, you can use the dumps() function from the json module. The dumps() function takes in a Python object and returns the JSON string representation. In practice, however, you’ll need to convert not a single dictionary but a collection such as a list of dictionaries.

Let us take an example.

In this example, we first import the json module. Then, we create a list of dictionaries called data. Next, we use the json.dumps() function to convert the data list into a JSON string. Finally, we print the JSON string to the console.

Read More Here

Top comments (0)