Convert Python dictionary to JSON string

Converting Python Dictionaries to JSON Strings

In Python, we can use dictionaries to store key-value pairs. JSON is a lightweight data exchange format, so converting Python dictionaries to JSON strings is very common in real-world development. This article will introduce Python’s json library and how to use it to convert dictionaries to JSON data format.

Introduction to the json Library

Python comes with the json library, which provides many JSON operation APIs for quickly parsing and generating JSON strings. This library is integrated into Python 2.6 and above.

Using the json Library

  • json.dumps(): Converts a Python dictionary (or other Python data type) to a JSON string.
  • json.loads(): Parses a JSON string into a Python dictionary (or other Python data type).

  • json.dump(): Converts a Python dictionary (or other Python data type) to a JSON string and writes it to a file.

  • json.load(): Reads a JSON string from a file and parses it into a Python dictionary (or other Python data type).

Converting a Dictionary to a JSON String

Use the dumps() function in the json library to convert a Python dictionary (or other Python data type) to a JSON string. The sample code is as follows:

import json

data = {"name":"lijie", "age":18}
json_str = json.dumps(data)
print(json_str)

Output:

{"name": "lijie", "age": 18}

In this example, we define a dictionary named data, convert it to a JSON string, and finally use the print() function to output it.

Some Advanced Uses of the json Library

Converting JSON Strings to Python Dictionaries

The json library also provides multiple APIs for converting JSON strings to Python dictionaries. The most basic method is the loads() function. The example code is as follows:

import json

json_str = '{"name": "lijie", "age": 18}'
data = json.loads(json_str)
print(data)

Output:

{'name': 'lijie', 'age': 18}

In this example, we define a JSON string named json_str, use the loads() function in the json library to convert it to a Python dictionary, and use the print() function to output it.

Writing to a JSON File

The json library also provides a dump() function that can convert a Python dictionary (or other Python data types) into a JSON string and write it to a file. The sample code is as follows:

import json

data = {“name”:”lijie”, “age”:18}

with open(‘data.json’, ‘w’) as file:
json.dump(data, file)

In the above sample code, we define a dictionary named data and use the dump() function in the json library to convert it to a JSON string. We then write the string to a file named “data.json”. The with open() statement indicates that the file will be automatically closed.

Reading Data from a JSON File

The json library also provides a load() function that can read a JSON string from a file and parse it into a Python dictionary (or other Python data type). The sample code is as follows:

import json

with open(‘data.json’, ‘r’) as file:
data = json.load(file)
print(data)

In the above sample code, we use the with open() statement to open the file named “data.json,” then use the load() function in the json library to read it and parse it into a Python dictionary (or other Python data type). Finally, we use the print() function to output the data.

Conclusion

This article introduced some basic uses of the json library. You also learned how to use the json library to convert Python dictionaries to JSON strings and other data types to JSON strings. We also learned how to write the converted JSON strings to files and how to read data from files and parse it into Python dictionaries (or other Python data types). I hope you find this article helpful.

Leave a Reply

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