Python Classroom Series – 02/Jul/2021

JSON (Java Script Object Notation)

  • This is very popular data interchange format.
  • JSON is all about writing the data in name value pair (JSON is collection of name value pairs)
<name> : <value>
  • Lets try to think about a product in the inventory, lets write
name
id
price
quantity
  • Now lets try to do the same with values
'name' : 'iphone'
'price': 120000
'id': 'mob0001'
'quantity': 50
  • Now lets try to understand different possibilities for values
name: <value>
      <text, boolean, number, list, object>

# text
'name' : 'iphone'
# boolean
'isOnline': false
# number:
'quantity': 50
# list
'tags': ['electronic', 'mobile', 'smartphone']
  • An object is json is collection of name value pairs
{
    "name": "iphone",
    "price": 120000,
    "id": "mob0001",
    "quantity": 50
}
  • Lets try to write a json file representing movies in a movie booking sit
{
  "movies": [
		{
			"title": "Avengers The End Game",
			"genre" : ["SciFic", "Adventure"],
			"rating" : 4.9
		},
		{
			"title": "Wonder Woman",
			"genre" : ["SciFic", "Adventure"],
			"rating" : 4.7
		},
		{
			"title": "Star Wars The Force Awakens",
			"genre" : ["SciFic", "Adventure"],
			"rating" : 4.7
		}
		
	]
}
  • Exercise: Write a Json representing bus in a bus booking site reprsenting
    • source and destination
    • bus type
    • Organization
    • stops
{
	"buslist": [{
			"source": "Hyderabad",
			"destination": "Bangalore",
			"type": "sleeper",
			"org": "Morning Star",
			"via": ["Kurnool", "Anantapur"]

		},
		{
			"source": "Bangalore",
			"destination": "Vijayawada",
			"type": "seater",
			"org": "Orange travels",
			"via": ["Tirupati", "Nellore", "Ongole"]

		},
		{
			"source": "Vijayawada",
			"destination": "Vishakapatnam",
			"type": "seater",
			"org": "APSRTC",
			"via": ["eluru", "Rajahmundry"]

		}
	]
}
  • Lets write a very simple python program which reads from a json file
    • Refer Here for the sample code for reading the data from json file.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin