"""
json full form "JavaScript Object notation".
Inspired by java script but now independent to any programming language
In json most frequent functions are 4 (load, loads, dump, dumps)
"""
# python code to read json file data Example 1
"""
Note:1 In Notepad/Notepad++ save below data with extension .json
while saving we can provide any name.
save the file, where the source(.py) file you are supposed to write,
I mean in the same directory)"""
{"Narendra":[{"id":"HM0002162","Working in HM": " past7 years",
"Designation":"Test Module Lead", "BU":"PES"},
{"Father's Name":"Udaya Bhaskara rao gaaru",
"Address":["Urivi village","Pedana Mandal",
"Krishna D.t", "A.P"],
"Qualification": "M.tech"}],
"Surendra":{"id":"HM0004xx", "age": 32, "Mobile_no": "9959xxx530"},
"Idly_cost": 30,
"Dosa_cost": 35,
"Puri_cost": 30}
"""
"""json.load() is used to read the dictionary data, from JSON file """
import json
# from json import load
with open("any_name.json") as data_file:
data = json.load(data_file)
print(data)
""" or """
# with open("any_name.json") as data_file:
# data = load(data_file)
print(data["Narendra"]) # getting all the values of narendra
print(data["Narendra"][0]["id"])
print(data["Narendra"][0]["Designation"])
print(data["Narendra"][1]["Father's Name"])
surendr_details = data['Surendra']
print(surendr_details)
s_id = data['Surendra']["id"]
print(s_id)
print(data["Dosa_cost"])
""" json.loads() is used to convert the JSON String into the Python dictionary."""
# person = '{"name": "Bob", "languages": ["English", "Fench"]}'
# person_dict = json.loads(person)
# print(person_dict)
# print(person_dict['languages'])
# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
# Output: ['English', 'French']
"""**********************************************************************"""
"""json.dump() method can be used for writing data into JSON file.
Syntax: json.dump(dict, file_pointer)"""
""" dump() method is used when the Python objects have to be stored in a file.
dump() needs the json file name in which the output has to be stored as an argument."""
# Data to be written
dictionary = {
"name": "Mahalakshmi Boyina",
"D.O.B": "10-Feb-2020",
"Blood group": "B+ve",
"phonenumber": "9000 30 1444"
}
# with open("sample.json", "w") as outfile:
# json.dump(dictionary, outfile)
"""
json.dumps() method can convert a Python object into a JSON string.
Syntax: json.dumps(dict, indent)
dumps() is used when the objects are required to be in string format(used for parsing, printing,...)
dumps() does not require any such file name to be passed.
"""
# Data to be written
dictionary = {
"Emp Id": "HM0002162",
"Name": "Narendra",
"Qualification": "M.Tech",
"Designation": "Test Module Lead"
}
# json_object = json.dumps(dictionary, indent=4)
# print(json_object, "\n", type(json_object))
# python code to read JSON file data Example 2
# copy below lines to a notepad and save with (.json)Extention. Ex: config.json
========================================================================
# Python code: Copy the following code and save the file (Narendra.py)
# copy below lines to a notepad and save with (.json)Extention. Ex: config.json
{ "Local_Machine_Path": "/home/narendrab/Screen_Capture_Working_V2/", "Linux_Remote_Machine_Path": "/home/Bhagyasree/ntest/", "Linux_Remote_Machine_IP_Addr": "172.116.16.2", "Linux_Remote_Machine_User_Name": "Bhagyasree", "Linux_Remote_Machine_Pass_word": "Nanna@123!!", }
========================================================================
# Python code: Copy the following code and save the file (Narendra.py)
import json json_file = "config.json" def get_entire_json_data(json_file): with open(json_file) as data_file: service_data = json.load(data_file) # pprint(service_data) # it will print entire data present in the json file return service_data x = get_entire_json_data(json_file) # print x Linux_Remote_Machine_IP_Addr = str(x["Linux_Remote_Machine_IP_Addr"]) # print(Linux_Remote_Machine_IP_Addr) Linux_Remote_Machine_User_Name = str(x["Linux_Remote_Machine_User_Name"]) # print(Linux_Remote_Machine_User_Name) Linux_Remote_Machine_Pass_word = str(x["Linux_Remote_Machine_Pass_word"]) # print(Linux_Remote_Machine_Pass_word)
Local_Machine_Path = str(x["Local_Machine_Path"]) # print(Local_Machine_Path) Linux_Remote_Machine_Path = str(x["Linux_Remote_Machine_Path"]) # print(Linux_Remote_Machine_Path)
Request: If you find this information useful, please provide your valuable comments