In this article you will learn how to convert python to json string.
The code below demonstrates it:
import json
person_dict = {
"name": "vincent",
"age" : 24,
"profession":"software_Developer"
}
#converting dictionary to json
person_json = json.dumps(person_dict)
print(person_dict)
We import the json module, which provides functions for working with JSON data and json.dumps() function to convert the person_dict dictionary into a JSON string.
The json.dumps() function takes a Python object (in this case, the dictionary) and returns a JSON-formatted string representation of that object. In this code, the resulting JSON string is assigned to the variable person_json.
person_dict dictionary, displays the contents of the dictionary, showing the key-value pairs and their corresponding values.
output:{'name': 'vincent', 'age': 24, 'profession': 'software_Developer'}
Top comments (0)