[
Create JSON String¶
Let us understand how to define JSON strings with in programs itself. Later we will see different ways of storing JSON data in files.
We will see following examples of JSON strings.
- Single JSON document.
- Multiple JSON documents, with one JSON per line.
- Multiple JSON documents as an Array under one attribute. Most of the REST APIs which return multiple elements follow this approach.
Single JSON document¶
Let us go through the details of Single JSON document.
- A single JSON can be in one line or are part of multiple lines.
- Here is an example of a single JSON as string in one line.
In [1]:
person = '{"id":1,"first_name":"Frasco","last_name":"Necolds","email":"fnecolds0@vk.com","gender":"Male","ip_address":"243.67.63.34"}'
In [2]:
type(person)
Out[2]:
str
- Here is an example of a single JSON as string that is part of multiple lines.
In [3]:
person = '''{
"id":1,
"first_name":"Frasco",
"last_name":"Necolds",
"email":"fnecolds0@vk.com",
"gender":"Male",
"ip_address":"243.67.63.34"
}'''
In [4]:
type(person)
Out[4]:
str
Multiple JSON Documents – One per line¶
Let us go through the details of multiple JSON Documents, with one JSON per line.
- All the lines will be part of one string.
- Each JSON will be separated by a new line. It is also known as new line character (
\n
). - Each line should contain one valid JSON document.
- There should not be a comma or any other character at the end of each line.
- Here is an example of representing multiple json documents as strings where each line will contain one JSON.
In [5]:
persons = '''{"id":1,"first_name":"Frasco","last_name":"Necolds","email":"fnecolds0@vk.com","gender":"Male","ip_address":"243.67.63.34"}
{"id":2,"first_name":"Dulce","last_name":"Santos","email":"dsantos1@mashable.com","gender":"Female","ip_address":"60.30.246.227"}
{"id":3,"first_name":"Prissie","last_name":"Tebbett","email":"ptebbett2@infoseek.co.jp","gender":"Genderfluid","ip_address":"22.21.162.56"}
{"id":4,"first_name":"Schuyler","last_name":"Coppledike","email":"scoppledike3@gnu.org","gender":"Agender","ip_address":"120.35.186.161"}
{"id":5,"first_name":"Leopold","last_name":"Jarred","email":"ljarred4@wp.com","gender":"Agender","ip_address":"30.119.34.4"}
{"id":6,"first_name":"Joanna","last_name":"Teager","email":"jteager5@apache.org","gender":"Bigender","ip_address":"245.221.176.34"}
{"id":7,"first_name":"Lion","last_name":"Beere","email":"lbeere6@bloomberg.com","gender":"Polygender","ip_address":"105.54.139.46"}
{"id":8,"first_name":"Marabel","last_name":"Wornum","email":"mwornum7@posterous.com","gender":"Polygender","ip_address":"247.229.14.25"}
{"id":9,"first_name":"Helenka","last_name":"Mullender","email":"hmullender8@cloudflare.com","gender":"Non-binary","ip_address":"133.216.118.88"}
{"id":10,"first_name":"Christine","last_name":"Swane","email":"cswane9@shop-pro.jp","gender":"Polygender","ip_address":"86.16.210.164"}'''
In [6]:
type(persons)
Out[6]:
str
Multiple JSON Documents – Array¶
Let us go through the details of multiple JSON Documents as an array.
- The entire array of JSON documents will be represented as one JSON and one string.
- The JSON document with all the elements can be part of multiple lines.
- The JSON can be represented as array with out any attribute or it can be represented as JSON with one attribute. The value of this attribute will be JSON Array.
- Here is an example of representing multiple JSON documents as JSON Array.
In [7]:
persons = '''[{"id":1,"first_name":"Frasco","last_name":"Necolds","email":"fnecolds0@vk.com","gender":"Male","ip_address":"243.67.63.34"},
{"id":2,"first_name":"Dulce","last_name":"Santos","email":"dsantos1@mashable.com","gender":"Female","ip_address":"60.30.246.227"},
{"id":3,"first_name":"Prissie","last_name":"Tebbett","email":"ptebbett2@infoseek.co.jp","gender":"Genderfluid","ip_address":"22.21.162.56"},
{"id":4,"first_name":"Schuyler","last_name":"Coppledike","email":"scoppledike3@gnu.org","gender":"Agender","ip_address":"120.35.186.161"},
{"id":5,"first_name":"Leopold","last_name":"Jarred","email":"ljarred4@wp.com","gender":"Agender","ip_address":"30.119.34.4"},
{"id":6,"first_name":"Joanna","last_name":"Teager","email":"jteager5@apache.org","gender":"Bigender","ip_address":"245.221.176.34"},
{"id":7,"first_name":"Lion","last_name":"Beere","email":"lbeere6@bloomberg.com","gender":"Polygender","ip_address":"105.54.139.46"},
{"id":8,"first_name":"Marabel","last_name":"Wornum","email":"mwornum7@posterous.com","gender":"Polygender","ip_address":"247.229.14.25"},
{"id":9,"first_name":"Helenka","last_name":"Mullender","email":"hmullender8@cloudflare.com","gender":"Non-binary","ip_address":"133.216.118.88"},
{"id":10,"first_name":"Christine","last_name":"Swane","email":"cswane9@shop-pro.jp","gender":"Polygender","ip_address":"86.16.210.164"}]'''
In [8]:
persons = '''[
{
"id": 1,
"first_name": "Frasco",
"last_name": "Necolds",
"email": "fnecolds0@vk.com",
"gender": "Male",
"ip_address": "243.67.63.34"
},
{
"id": 2,
"first_name": "Dulce",
"last_name": "Santos",
"email": "dsantos1@mashable.com",
"gender": "Female",
"ip_address": "60.30.246.227"
},
{
"id": 3,
"first_name": "Prissie",
"last_name": "Tebbett",
"email": "ptebbett2@infoseek.co.jp",
"gender": "Genderfluid",
"ip_address": "22.21.162.56"
},
{
"id": 4,
"first_name": "Schuyler",
"last_name": "Coppledike",
"email": "scoppledike3@gnu.org",
"gender": "Agender",
"ip_address": "120.35.186.161"
},
{
"id": 5,
"first_name": "Leopold",
"last_name": "Jarred",
"email": "ljarred4@wp.com",
"gender": "Agender",
"ip_address": "30.119.34.4"
},
{
"id": 6,
"first_name": "Joanna",
"last_name": "Teager",
"email": "jteager5@apache.org",
"gender": "Bigender",
"ip_address": "245.221.176.34"
},
{
"id": 7,
"first_name": "Lion",
"last_name": "Beere",
"email": "lbeere6@bloomberg.com",
"gender": "Polygender",
"ip_address": "105.54.139.46"
},
{
"id": 8,
"first_name": "Marabel",
"last_name": "Wornum",
"email": "mwornum7@posterous.com",
"gender": "Polygender",
"ip_address": "247.229.14.25"
},
{
"id": 9,
"first_name": "Helenka",
"last_name": "Mullender",
"email": "hmullender8@cloudflare.com",
"gender": "Non-binary",
"ip_address": "133.216.118.88"
},
{
"id": 10,
"first_name": "Christine",
"last_name": "Swane",
"email": "cswane9@shop-pro.jp",
"gender": "Polygender",
"ip_address": "86.16.210.164"
}
]'''
Multiple JSON Documents – One Attribute¶
Here is an example of representing multiple JSON documents as part of single document with single attribute where value is of type JSON Array.
In [9]:
persons = '''{
"results": [
{
"id": 1,
"first_name": "Frasco",
"last_name": "Necolds",
"email": "fnecolds0@vk.com",
"gender": "Male",
"ip_address": "243.67.63.34"
},
{
"id": 2,
"first_name": "Dulce",
"last_name": "Santos",
"email": "dsantos1@mashable.com",
"gender": "Female",
"ip_address": "60.30.246.227"
},
{
"id": 3,
"first_name": "Prissie",
"last_name": "Tebbett",
"email": "ptebbett2@infoseek.co.jp",
"gender": "Genderfluid",
"ip_address": "22.21.162.56"
},
{
"id": 4,
"first_name": "Schuyler",
"last_name": "Coppledike",
"email": "scoppledike3@gnu.org",
"gender": "Agender",
"ip_address": "120.35.186.161"
},
{
"id": 5,
"first_name": "Leopold",
"last_name": "Jarred",
"email": "ljarred4@wp.com",
"gender": "Agender",
"ip_address": "30.119.34.4"
},
{
"id": 6,
"first_name": "Joanna",
"last_name": "Teager",
"email": "jteager5@apache.org",
"gender": "Bigender",
"ip_address": "245.221.176.34"
},
{
"id": 7,
"first_name": "Lion",
"last_name": "Beere",
"email": "lbeere6@bloomberg.com",
"gender": "Polygender",
"ip_address": "105.54.139.46"
},
{
"id": 8,
"first_name": "Marabel",
"last_name": "Wornum",
"email": "mwornum7@posterous.com",
"gender": "Polygender",
"ip_address": "247.229.14.25"
},
{
"id": 9,
"first_name": "Helenka",
"last_name": "Mullender",
"email": "hmullender8@cloudflare.com",
"gender": "Non-binary",
"ip_address": "133.216.118.88"
},
{
"id": 10,
"first_name": "Christine",
"last_name": "Swane",
"email": "cswane9@shop-pro.jp",
"gender": "Polygender",
"ip_address": "86.16.210.164"
}
]
}'''
]