Json Commands
Last Update: July.2024
In this page i will write some commands that are inportant and also to keep remmebed for “json file”.
Commands are not sorted.
json file format:
Here is a simple json file format:
myjson =[
{
“info”:{
“Project Name”:”Project Test Name”,
“Start Date”:”1.5.2024″,
“End Date”:”30.8.2024″
},
“Tasks”:{
“Task Title”:”This is Task No. 1″,
“Assigned to”:”Robert”,
“TSDate”:”any start Date”,
“TEDate”:” any END Date”,
}}
]
# Code to re-call a json file as API
import requests
# The API endpoint
url = “https://api.jsonsilo.com/public/ c09f41c5-fcec-4fa3-af32-454b9900eb25“
# api.jsonsilo.com/public/c09f41c5-fcec-4fa3-af32-454b9900eb25
# https://api.jsonsilo.com/public/c09f41c5-fcec-4fa3-af32-454b9900eb25
# A GET request to the API
response = requests.get(url)
# Print the response
mydata = response.json()
print(mydata)
import json
#convert json to str
dataNstr = json.dumps(mydata)
print(‘data size is > ‘,len(dataNstr),’ Characters’)
Now, we start the Commands..
Get Kesy in the json file:
for each in myjson :
print(each.keys())[Out Put]
Get all sub-keys in each main keys
for each in myjson :
print(each[“info”].keys())
print(each[“Tasks”].keys())
Get the Main Keys in json file :
def get_json_main_keys(the_data) :
“””
Function to read the json data-set and return the main keys as a list
“””
main_keys = []for each in the_data.keys():
main_keys.append(each) # Put the Keys in a list
return main_keys# To read the list
main_keys = get_json_main_key(myjson)
print(main_keys)
Get the Sub-Keys for each Main-Keys:
for each in mk :
print(each,” “,myjson[each].keys())also:
print(myjson[mk[0]].keys())
Code works Fine …
# To get all the keys and the data in each key:
def print_key_values_data(the_data,key):
if isinstance(mydata[key], list):
print(f’\n> This is the {key} key: ‘)
for i in range(len(mydata[key])) :
for z in range(len(list(mydata[key]))+1):
print(list(mydata[key][i].keys())[z],” : “,end=””)
print(list(mydata[key][i].values())[z])
print(“\n”)
else :
print(f’\n> This is the {key} key: ‘)
for i in range (len(mydata[key].keys())) :
print(list(mydata[key].keys())[i],” : “,end=””)
print(list(mydata[key].values())[i])
print(“\n\n”)
Code works fine ..
def get_about_json(myjson) :
# get json info
print(f” json data type {type(myjson)}”)
print(f” The json data has {len(myjson.keys())} keys, they are as {myjson.keys()}”)
for x in range (len(myjson.keys())):
print(f”\n The key {list(myjson.keys())[x]}, is from {type(myjson[list(myjson.keys())[x]])} type, and has {len(list(myjson.keys())[x])-1} attribute”)
if isinstance(myjson[list(myjson.keys())[x]], list) :
print(‘ Thy are:’,myjson[list(myjson.keys())[x]][0].keys())
else:
print(f” Thy are: {myjson[list(myjson.keys())[x]].keys()}”)
get_about_json(mydata)
#Code to get one record from the json data
def get_record(the_data,the_key,attrib,value_tofinde) :
# Find tasks with task_id = 34 tasks_details = [task for task in the_data[the_key] if task.get(attrib) == value_tofinde] for t in range (len(tasks_details[0].keys())) : print(list(tasks_details[0].keys())[t]," : ",end="") print(list(tasks_details[0].values())[t]) print("\n")get_record(mydata,’tasks’,’Task-Title’,’Task 3′)


