In: Computer Science
Python programming
*****************************************************************************************************
If we load the json files then how could we use the objects to extract particular values from those files.
eg:
fi1 = open("king.json", "rb")
obj1 = yaml.safe_load(fi1.read())
fi1.close()
fi2 = open("queen.json", "rb")
obj2 = yaml.safe_load(fi2.read())
fi2.close()
Now if the JSON file queen.json had elements like name, rule year, children, etc. and we wanted the name of the queen who ruled between particular years, how would we do it?
second would be getting a value common for both files? like the name of king and queen who ruled between specific years.
Please find below code in Python and Don't forget to give a Like.
Please refer screenshot for indentation and output and also json files i used.
Code:
import json year=int(input("Enter the year")) from_year,to_year=input("enter from year, to year with spaces: ").split() from_year=int(from_year) to_year=int(to_year) fil2=open("queen.json",'r') object2=json.load(fil2) list1=[],list2=[] for i in object2: if(i['rule_year']==year): print("The name of the Queen for particular year is ",i['name']) if(i['rule_year']>=from_year and i['rule_year']<=to_year): list1.append(i['name']) print("The names of queen for specific years",from_year,"to",to_year) for i in list1: print(i) fil2.close() fil1=open("king.json",'r') object1=json.load(fil1) for i in object1: if(i['rule_year']>=from_year and i['rule_year']<=to_year): list2.append(i['name']) print("The names of king for specific years",from_year,"to",to_year) for i in list2: print(i) fil1.close()
King.json file