Untitled Document
Working with JSON data using JSON Module
1. Loading JSON String into Python Object Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Loading JSON String into Python Object 
       
# Java Script Object Notation 
import json 

people_string = '''
{ 
  "people": [ 
     {
       "name": "Mangesh Pande",
       "phone": "123-556-698",
       "emails": ["mangeshpande@gmail.com","mangesh.pande@work-place.com"],
       "has_license": false
     },
     {
        "name": "Mrunmayee Pande",
        "phone": "562-887-9890",
        "emails": null,
        "has_license": true
     }
  ]
}

'''

data = json.loads(people_string)
print(type(data))
print(data)

# How Python Converts the Json data in Python Objects is documented on python docs.org
# https://docs.python.org/3/library/json.html - you can refer encoders and Decoders section 

print(data['people'])

# looping through the Json Object 

for person in data['people']:
	print(person)

# Printing Specific information from list

for person in data['people']:
	print(person['name'])
       
2. Program - Loading Python object in JSON String Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Loading Python object in JSON String 

# For This we will use dumps method

# In this Example First we will dump JSON string into python object and 
# then we will dump it back to JSON
# Here we will delete the phone numbers from Python Object and then we will dump it to JSON 
# Just to show some chnages .

import json 

people_string = '''
{ 
  "people": [ 
     {
       "name": "Mangesh Pande",
       "phone": "123-556-698",
       "emails": ["mangeshpande@gmail.com","mangesh.pande@work-place.com"],
       "has_license": false
     },
     {
        "name": "Mrunmayee Pande",
        "phone": "562-887-9890",
        "emails": null,
        "has_license": true
     }
  ]
}

'''

data = json.loads(people_string)

for person in data['people']:
	del person['phone']

new_string = json.dumps(data)
print(new_string)

# Now lets do some indentation file dumping the data in JSON Object 

new_string = json.dumps(data, indent=2)
print(new_string)

# If you want you can sort the keys 

new_string = json.dumps(data, indent=2, sort_keys=True)
print(new_string)
       
3. Program - Lodaing JSON file into Python Object Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Lodaing JSON file into Python Object 

# To load a file we will use json.load

import json

with open('states.json') as f:
	data = json.load(f)

for state in data['states']:
	print(state)
	# Printing same key from Dictionary 
	print(state['name'], state['abbreviation'])
    
4. Program - Lodaing Python Object in JSON file Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Lodaing Python Object in JSON file 

# To load python object in Json file we will use dump method 

import json

with open('states.json') as f:
	data = json.load(f)

for state in data['states']:
	del state['area_codes']

with open('new_states.json', 'w') as f:
	json.dump(data, f, indent=2)
       
5. Program - Parsing Information from Content Downloaded from WEB URL Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program -  Parsing Information from Content Downloaded from WEB URL

# Go throug the video after 14 mins till end to undertsnad the concept 

import json 
from urllib.request import urlopen

with urlopen("https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json") as response:
	source = response.read()

data = json.loads(source)

print(json.dumps(data, indent=2))
       
Untitled Document