EN
Python - convert string to json
0 points
In this article, we would like to show you how to convert string to JSON in Python.
Quick solution:
xxxxxxxxxx
1
import json
2
3
string = '{"id": 2, "name": "Tom", "age": 25}'
4
result = json.loads(string)
In this example, we use json.loads()
method to convert string to the JSON.
xxxxxxxxxx
1
import json
2
3
string = '{"id": 2, "name": "Tom", "age": 25}'
4
result = json.loads(string)
5
6
print(result)
7
print(result["id"], result["name"], result["age"])
Output:
xxxxxxxxxx
1
{'id': 2, 'name': 'Tom', 'age': 25}
2
2 Tom 25