The json.dumps function in Python is used to convert a Python object into a JSON-encoded string. In order to transform a JSON String into a JSON object, we need to use the json.loads function.
Here is an example:
import json
json_string = '{"name": "John", "age": 30, "city": "New York"}'
# Convert JSON string to Python object (dictionary)
json_object = json.loads(json_string)
print(json_object)
Output:
{'name': 'John', 'age': 30, 'city': 'New York'}
In the above code, we first import the json module. Then, we define a JSON string and assign it to the variable jsonstring. We then use the json.loads function to convert this JSON string into a Python object (in this case, a dictionary) and assign it to the variable jsonobject. Finally, we print the contents of the json_object.
Asked: 2023-06-07 00:58:19 +0000
Seen: 12 times
Last updated: Jun 07 '23