Answers for "how to use json"

11

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" }
			]
	}
]
Posted by: Guest on July-27-2020
0

how to use json

#first save a JSON file in the same directory
#in the json type {} inside also

import JSON

#getting data from the json
def get_data():
	with open("json_name.json", "r") as f:
		data = json.load(f)
	return data

#saving data
def save_data(data):
	with open("json_name.json", "w") as f:
		json.dump(data, f)
#this is how we get data in json
data = get_data()
#this is how we change values in the json
data['test'] = {'name': 'Siamese'}
#this is how we save the changes in the json
save_data(data)

data = get_data()
print(data['test']['name'])
#output will be "Siamese"
Posted by: Guest on June-28-2021
0

how to set up a json file

{
	"name" : "data"
}
Posted by: Guest on April-20-2020
0

how to set up a json file

{
	
}
Posted by: Guest on April-20-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language