__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Techno Solutions
# -----------------------------------------------------------------------------
"""
JSON is the full form of JavaScript Object Notation.
Inspired by JavaScript but now independent of any programming language
In JSON, the most frequent functions are 4 (load, loads, dump, dumps)
"""
from Class_27_OOPs_4_Inheritance.OOPS_4_data_abstration import ganesh
"""
Note:1 In Notepad/Notepad++, save the data with the 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 ""
"""json.load() is used to read the dictionary data from a JSON file """
import json # it's an internal module
from json import load,dump
from pprint import pprint
# f_o = open("ganesh.txt","w+")
# f_o.write("used to read the dictionary data")
# f_o.close()
# #
# # #
# with open("raahi.txt", "w+") as f_obj:
# f_obj.write("Notepad/Notepad++ save below data with extension")
with open("hotel_input.json") as data_file:
data = json.load(data_file)
# print(data) # {'Narendra': [{'id': 'HM0002162', 'Working in HM': ' past9 years', 'Designation': 'Team 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': 45, 'Dosa_cost': 100, 'Puri_cost': 100, 'raagi_jaava': 40}
# pprint(data)
print(data["Idly_cost"])
print(data["Puri_cost"])
designation = data["Narendra"][0]["Designation"]
print(designation)
print(data["Narendra"][1]["Address"])
with open("Firmware_info.json") as data_file:
data = json.load(data_file)
""" or """
with open("hotel_input.json") as data_file:
data = load(data_file)
# print(data)
# print(data["Narendra"]) # getting all the values of narendra
# print(data["Narendra"][1])
# print(data["Narendra"][0]["id"])
# designation = data["Narendra"][0]["Designation"]
# print(designation)
# f_name = data["Narendra"][1]["Father's Name"]
# print(f_name)
# surendr_details = data['Surendra']
# print(surendr_details)
# s_id = data['Surendra']["Mobile_no"]
# print(s_id)
# print(data["Dosa_cost"])
# print(data["vinod_info"])
with open("iv_mainline.json") as data_obj:
data = json.load(data_obj)
# print(data)
# print(data["Promotion_Status"])
# print(data["FirmWare"])
# print(data["FirmWare"][1])
# print(data["FirmWare"][1]["MI300A"])
""" json.loads() is used to convert the JSON String into a Python dictionary."""
person = '{"name": "kruthika", "languages": ["Tamil","English", "Telugu"]}' # json string --> json data present inside string
# person_dict = json.loads(person)
# print(person_dict, type(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 a JSON file.
Syntax: json.dump(dict, file_pointer)"""
"""The dump() method is used when the Python objects have to be stored in a file.
The dump() needs the JSON file name in which the output has to be stored as an argument."""
# Data to be written
maha_info = {
"name": "Mahalakshmi Boyina",
"D.O.B": "10-Feb-2020",
"Blood group": "B +ve",
"phonenumber": "9000 30 1444"
}
with open("nanditha.json", "w+") as outfile:
json.dump(maha_info, outfile)
data = {"FirmWare": [
{
"MI200": "SMU:68.61; SDMA:08"
},
{
"MI300A": "SMU: 04.85.103.00 ; SDMA:19"
},
{
"MI300X": "SMU: 00.85.110.00 ; SDMA:19"
},
{
"NAVI31": "SMU:78.126; SDMA:24"
},
{
"NAVI32": "SMU:80.80; SDMA:25"
}
]
}
# with open("Firmware_info.json", "w") as outfile:
# json.dump(data, outfile)
"""
json.dumps() method can convert a Python dictionary 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
content = {
"Emp Id": "HM0002162",
"Name": "Narendra",
"Qualification": "M.Tech",
"Designation": "Test Module Lead"
}
# json_object = json.dumps(data, indent=4) # indent is used to maintain indentation for output
# print(json_object, "\n", type(json_object))
# with open("Mahalakshmi.json") as M_Obj:
# ganesh =json.load(M_Obj)
# Mahalakshmi boyina's data of bith is 10-Feb-2020
# print(ganesh["name"],"'s","Date of birth is", ganesh["D.O.B"])
maha_info = {
"name": "Mahalakshmi Boyina",
"D.O.B": "10-Feb-2020",
"Blood group": "B +ve",
"phonenumber": "9000 30 1444"
}
from json import load, dump
with open("Mahalakshmi.json","w+") as jf_obj:
dump(maha_info, jf_obj)
with open("Mahalakshmi.json") as jf_obj_1:
data = load(jf_obj_1)
print(data["D.O.B"])Author: Boyina Narendra
Supporting Author: M. Meera Sindhu
Request: If you find this information useful, please provide your valuable comments
Sunday, May 24, 2026
Python Material - Part - 27 - json_Module
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment