Python JSON Examples

JSON has become the de facto standard for information exchange and Python makes it easy to work with JSON format with a built-in library called json.

Convert JSON to string

There are 2 ways to convert JSON to a string, str() and json.dumps() method. json.dumps() is recommended because it produces a valid JSON string.

import json
data = [
{
"name": "Ellie Becker",
"info": {
"title": "Ms.",
"age": 56,
"phone": "(572) 478-1669",
"email": "[email protected]"
}
},
{
"name": "Sedrick Hane",
"info": {
"title": "Dr.",
"age": 51,
"phone": "+18435906060",
"email": "[email protected]"
}
},
{
"name": "Caroline Grimes",
"info": {
"title": "Miss",
"age": 23,
"phone": "(406) 966-4774 x059",
"email": "[email protected]"
}
},
{
"name": "Alberta Rosenbaum",
"info": {
"title": "Mrs.",
"age": 59,
"phone": "+1-793-959-9163",
"email": "[email protected]"
}
},
{
"name": "Pearline Altenwerth",
"info": {
"title": "Prof.",
"age": 53,
"phone": "1-997-437-7439 x512",
"email": "[email protected]"
}
},
{
"name": "Jettie Stoltenberg",
"info": {
"title": "Ms.",
"age": 31,
"phone": "345.540.3284",
"email": "[email protected]"
}
},
{
"name": "Kailyn Weber",
"info": {
"title": "Mr.",
"age": 38,
"phone": "719-817-6214 x2544",
"email": "[email protected]"
}
},
{
"name": "Graham Dietrich",
"info": {
"title": "Miss",
"age": 53,
"phone": "+1.623.808.4539",
"email": "[email protected]"
}
},
{
"name": "Lydia Gottlieb",
"info": {
"title": "Mr.",
"age": 22,
"phone": "+1-205-903-5351",
"email": "[email protected]"
}
},
{
"name": "Conrad Ziemann",
"info": {
"title": "Mr.",
"age": 24,
"phone": "375.406.8412 x4902",
"email": "[email protected]"
}
},
{
"name": "Caesar Koch",
"info": {
"title": "Ms.",
"age": 39,
"phone": "+1-601-513-6516",
"email": "[email protected]"
}
}
]
json_string = json.dumps(data)

Output

"[{'name': 'Ellie Becker', 'info': {'title': 'Ms.', 'age': 56, 'phone': '(572) 478-1669', 'email': '[email protected]'}}, {'name': 'Sedrick Hane', 'info': {'title': 'Dr.', 'age': 51, 'phone': '+18435906060', 'email': '[email protected]'}}, {'name': 'Caroline Grimes', 'info': {'title': 'Miss', 'age': 23, 'phone': '(406) 966-4774 x059', 'email': '[email protected]'}}, {'name': 'Alberta Rosenbaum', 'info': {'title': 'Mrs.', 'age': 59, 'phone': '+1-793-959-9163', 'email': '[email protected]'}}, {'name': 'Pearline Altenwerth', 'info': {'title': 'Prof.', 'age': 53, 'phone': '1-997-437-7439 x512', 'email': '[email protected]'}}, {'name': 'Jettie Stoltenberg', 'info': {'title': 'Ms.', 'age': 31, 'phone': '345.540.3284', 'email': '[email protected]'}}, {'name': 'Kailyn Weber', 'info': {'title': 'Mr.', 'age': 38, 'phone': '719-817-6214 x2544', 'email': '[email protected]'}}, {'name': 'Graham Dietrich', 'info': {'title': 'Miss', 'age': 53, 'phone': '+1.623.808.4539', 'email': '[email protected]'}}, {'name': 'Lydia Gottlieb', 'info': {'title': 'Mr.', 'age': 22, 'phone': '+1-205-903-5351', 'email': '[email protected]'}}, {'name': 'Conrad Ziemann', 'info': {'title': 'Mr.', 'age': 24, 'phone': '375.406.8412 x4902', 'email': '[email protected]'}}, {'name': 'Caesar Koch', 'info': {'title': 'Ms.', 'age': 39, 'phone': '+1-601-513-6516', 'email': '[email protected]'}}]"

Convert JSON string to dict

To convert a JSON string to a dictionary using json.loads() which accepts a valid json string and returns a dictionary.

import json
json_string = '{"success": "true", "message": "API works"}'
data = json.loads(json_string)
print(data['message'])
#output: "API works"

Leave a Comment

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