json files code example

Example 1: json data example

[
	{
		"id": "0001",
		"type": "donut",
		"name": "Cake",
		"ppu": 0.55,
		"batters":
			{
				"batter":
					[
						{ "id": "1001", "type": "Regular" },
						{ "id": "1002", "type": "Chocolate" },
						{ "id": "1003", "type": "Blueberry" },
						{ "id": "1004", "type": "Devil's Food" }
					]
			},
		"topping":
			[
				{ "id": "5001", "type": "None" },
				{ "id": "5002", "type": "Glazed" },
				{ "id": "5005", "type": "Sugar" },
				{ "id": "5007", "type": "Powdered Sugar" },
				{ "id": "5006", "type": "Chocolate with Sprinkles" },
				{ "id": "5003", "type": "Chocolate" },
				{ "id": "5004", "type": "Maple" }
			]
	},
	{
		"id": "0002",
		"type": "donut",
		"name": "Raised",
		"ppu": 0.55,
		"batters":
			{
				"batter":
					[
						{ "id": "1001", "type": "Regular" }
					]
			},
		"topping":
			[
				{ "id": "5001", "type": "None" },
				{ "id": "5002", "type": "Glazed" },
				{ "id": "5005", "type": "Sugar" },
				{ "id": "5003", "type": "Chocolate" },
				{ "id": "5004", "type": "Maple" }
			]
	},
	{
		"id": "0003",
		"type": "donut",
		"name": "Old Fashioned",
		"ppu": 0.55,
		"batters":
			{
				"batter":
					[
						{ "id": "1001", "type": "Regular" },
						{ "id": "1002", "type": "Chocolate" }
					]
			},
		"topping":
			[
				{ "id": "5001", "type": "None" },
				{ "id": "5002", "type": "Glazed" },
				{ "id": "5003", "type": "Chocolate" },
				{ "id": "5004", "type": "Maple" }
			]
	}
]

Example 2: python json file operations

import json # Imports the json file that we need
with open("jsonfile.json", "r") as jsonFile: # Opens Json file for reading
	data = json.load(jsonFile) # Reads Json file using json.load() method
'''
Example Json File:
[{"name": "Bill", "age": 20},
{"name": "Mary", "age": 31},
{"name": "Jake", "age": 19}]
'''
data[1]["age"] = 32 # Alters data variable
''' 
Now the data variable is:
[{"name": "Bill", "age": 20},
{"name": "Mary", "age": 32},
{"name": "Jake", "age": 19}]
'''
with open("jsonfile.json", "w") as fileJson: # Opens Json file once again
  json.dump(data, fileJson) # Replaces the Json file using json.dump() method

Example 3: javascript object notation

{
  "first_name": "Taylor",
  "last_name": "Hawkes",
  "age": 31,
  "address": {
    "street": "954 Kazaam Lane",
    "city": "Boulder",
    "state": "CO",
    "postalCode": "80303"
  },
  "emails": [
    {
      "type": "main",
      "number": "[email protected]"
    },
    {
      "type": "secondary",
      "number": "[email protected]"
    }
  ]
}

Example 4: download .json file

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}

Example 5: what is JSON

JavaScript Object Notation 
	Its a key value pair 
	its popular light-weight way of transfering data 

	for example : 
	Lets try to create a json for Person Object 
			with name , age , isMarried , gender 

		Its ket value pair 
		the key is always String and need to be in quotation
		value can be :
			String 
			Number 
			Boolean 
			null 
			array 
			another json object


		This is one json with 5 fields 
		each key value pair should be separated by comma 
		{
			"name" 		: "Anna", 
			"age" 		: 18  , 
			"isMarried" : false , 
			"gender"	: "female", 
			"company"	: null 
		}

Example 6: JSON stands for

JSON = JavaScript Object Notation