Using JSON Data in Python
Breaking string data into dictionary object and viceversa using json library
data='{"bookno":"1234","title":"C Programming","author":"Dr B P Sharma","price":"340"}'
import json
# converting string to dictionary object
b=json.loads(data)
print(b['bookno'])
# converting a dictionary in string
json.dumps(b)
import json
# converting string to dictionary object
b=json.loads(data)
print(b['bookno'])
# converting a dictionary in string
json.dumps(b)
Working with Single JSON Record
Create a JSON file as book.json and place the following record into that file{"bookno":"1234","title":"C Programming","author":"Dr B P Sharma","price":"340"}
Create Python File to read the data as readbook.py
import json
f=open("book.json")
data=f.read()
d=json.loads(data)
for k,v in d.items():
print(k,"=",v)
Output
bookno = 1234
title = C Programming
author = Dr B P Sharma
price = 340
Working with Multiple JSON Records of single category
{
"books": [{
"bookno": "1234",
"title": "Java",
"author": "Gosling",
"price": "900"
},
{
"bookno": "1235",
"title": "PHP",
"author": "Rohit",
"price": "300"
},
{
"bookno": "1236",
"title": "Oracle",
"author": "Sanjay",
"price": "400"
}
]
}
import json
f=open("books.json")
data=f.read()
d=json.loads(data)
i=1
for k in d.keys():
records=d[k]
print("Record of",k.upper());
for r in records:
print("Record #{}".format(i))
for k,v in r.items():
print(k,"=",v)
i=i+1
Output
Record of BOOKS
Record #1
bookno = 1234
title = Java
author = Gosling
price = 900
Record #2
bookno = 1235
title = PHP
author = Rohit
price = 300
Record #3
bookno = 1236
title = Oracle
author = Sanjay
price = 400
Another Case
import json
f=open("books.json")
data=f.read()
d=json.loads(data)
books=d["books"]
for b in books:
print(b["bookno"],b["title"],b["author"],b["price"])
Output
1234 Java Gosling 900
1235 PHP Rohit 300
1236 Oracle Sanjay 400
Another Case
import json
f=open("books.json")
data=f.read()
d=json.loads(data)
books=d["books"]
for b in books:
print(b["bookno"],b["title"],b["author"],b["price"])
Output
1234 Java Gosling 900
1235 PHP Rohit 300
1236 Oracle Sanjay 400
Working with Multiple JSON Records of Multiple categories
Create a JSON file as library.json and place the following records in that file
{
"books": [{
"bookno": "1234",
"title": "Java",
"author": "Gosling",
"price": "900"
},
{
"bookno": "1235",
"title": "PHP",
"author": "Rohit",
"price": "300"
},
{
"bookno": "1236",
"title": "Oracle",
"author": "Sanjay",
"price": "400"
}
],
"students": [{
"rollno": "1001",
"name": "Rohit",
"course": "MCA"
},
{
"rollno": "1002",
"name": "Karan",
"course": "MBA"
}
],
"faculties": [{
"empid": "A001",
"name": "Sanjay Kumar"
},
{
"empid": "A002",
"name": "Ruchi Verma"
},
{
"empid": "A003",
"name": "Kanchan"
},
{
"empid": "A004",
"name": "Garima"
}
]
}
Create Python File to read the data as readlibrary.py
import json
f=open("library.json")
data=f.read()
d=json.loads(data)
for k in d.keys():
records=d[k]
i=1
print("Record of",k.upper());
for r in records:
print("Record #{}".format(i))
for k,v in r.items():
print(k,"=",v)
i=i+1
Output
Record of BOOKS
Record #1
bookno = 1234
title = Java
author = Gosling
price = 900
Record #2
bookno = 1235
title = PHP
author = Rohit
price = 300
Record #3
bookno = 1236
title = Oracle
author = Sanjay
price = 400
Record of STUDENTS
Record #1
rollno = 1001
name = Rohit
course = MCA
Record #2
rollno = 1002
name = Karan
course = MBA
Record of FACULTIES
Record #1
empid = A001
name = Sanjay Kumar
Record #2
empid = A002
name = Ruchi Verma
Record #3
empid = A003
name = Kanchan
Record #4
empid = A004
name = Garima
Comments
Post a Comment