I'm fairly new to programming, so please bear with me. I've been working on a project and am learning a huge about amount dicts and lists. I'm near the end, but have been stuck on a particular portion for a week now and could use some help.
I have a dict and a list, both with nested dicts of a large amount of data. I will provide samples:
neo_dict (these are only the first three nested dicts of about 40,000):
{
"2020 M3": {
"id": "dK20M030",
"spkid": "1003699",
"full_name": " C/2020 M3 (ATLAS)",
"pdes": "2020 M3",
"name": "ATLAS",
"prefix": "C",
"neo": "Y",
"pha": "",
"H": "",
"G": "",
"M1": "14.6",
"M2": "",
"K1": "6.5",
"K2": "",
"PC": "",
"diameter": "",
"extent": "",
"albedo": "",
"rot_per": "",
"GM": "",
"BV": "",
"UB": "",
"IR": "",
"spec_B": "",
"spec_T": "",
"H_sigma": "",
"diameter_sigma": "",
"orbit_id": "JPL 17",
"epoch": "2459177.5",
"epoch_mjd": "59177",
"epoch_cal": "20201124",
"equinox": "J2000",
"e": "0.952708189",
"a": "26.81750256",
"q": "1.26824827",
"i": "23.47352818",
"om": "71.2502355",
"w": "328.4462512",
"ma": "0.208483561",
"ad": "52.36675686",
"n": "0.007097029",
"tp": "2459148.124",
"tp_cal": "20201025.62",
"per": "50725.45171",
"per_y": "138.8787179",
"moid": "0.327377",
"moid_ld": "127.4053071",
"moid_jup": "0.886293",
"t_jup": "1.46",
"sigma_e": "2.52E-06",
"sigma_a": "0.0014408",
"sigma_q": "5.38E-07",
"sigma_i": "2.93E-05",
"sigma_om": "9.94E-06",
"sigma_w": "1.70E-05",
"sigma_ma": "1.68E-05",
"sigma_ad": "0.0028134",
"sigma_n": "5.72E-07",
"sigma_tp": "1.88E-05",
"sigma_per": "4.0879",
"class": "HTC",
"producer": "Otto Matic",
"data_arc": "265",
"first_obs": "6/27/2020",
"last_obs": "3/19/2021",
"n_obs_used": "2140",
"n_del_obs_used": "",
"n_dop_obs_used": "",
"condition_code": "3",
"rms": "0.48834",
"two_body": "",
"A1": "",
"A2": "",
"A3": "",
"DT": ""
},
"2020 P4-B": {
"id": "dK20P04b",
"spkid": "1003715",
"full_name": " C/2020 P4-B",
"pdes": "2020 P4-B",
"name": "",
"prefix": "C",
"neo": "Y",
"pha": "",
"H": "",
"G": "",
"M1": "16.2",
"M2": "",
"K1": "10",
"K2": "",
"PC": "",
"diameter": "",
"extent": "",
"albedo": "",
"rot_per": "",
"GM": "",
"BV": "",
"UB": "",
"IR": "",
"spec_B": "",
"spec_T": "",
"H_sigma": "",
"diameter_sigma": "",
"orbit_id": "JPL 1",
"epoch": "2459068.5",
"epoch_mjd": "59068",
"epoch_cal": "20200807",
"equinox": "J2000",
"e": "0.909209182",
"a": "1.020070968",
"q": "0.092613078",
"i": "28.14731293",
"om": "173.4267944",
"w": "171.6064847",
"ma": "358.9621325",
"ad": "1.947528857",
"n": "0.956661933",
"tp": "2459069.585",
"tp_cal": "20200808.08",
"per": "376.3084822",
"per_y": "1.030276474",
"moid": "0.135011",
"moid_ld": "52.54223087",
"moid_jup": "3.49537",
"t_jup": "5.426",
"sigma_e": "0.16589",
"sigma_a": "1.9184",
"sigma_q": "0.031723",
"sigma_i": "4.689",
"sigma_om": "14.935",
"sigma_w": "29.714",
"sigma_ma": "2.8976",
"sigma_ad": "3.6627",
"sigma_n": "2.6988",
"sigma_tp": "0.6924",
"sigma_per": "1061.6",
"class": "ETc",
"producer": "Davide Farnocchia",
"data_arc": "1",
"first_obs": "8/6/2020",
"last_obs": "8/7/2020",
"n_obs_used": "80",
"n_del_obs_used": "",
"n_dop_obs_used": "",
"condition_code": "9",
"rms": "0.079265",
"two_body": "",
"A1": "",
"A2": "",
"A3": "",
"DT": ""
},
"2020 P4-C": {
"id": "dK20P04c",
"spkid": "1003716",
"full_name": " C/2020 P4-C",
"pdes": "2020 P4-C",
"name": "",
"prefix": "C",
"neo": "Y",
"pha": "",
"H": "",
"G": "",
"M1": "15.6",
"M2": "",
"K1": "10",
"K2": "",
"PC": "",
"diameter": "",
"extent": "",
"albedo": "",
"rot_per": "",
"GM": "",
"BV": "",
"UB": "",
"IR": "",
"spec_B": "",
"spec_T": "",
"H_sigma": "",
"diameter_sigma": "",
"orbit_id": "JPL 1",
"epoch": "2459069.5",
"epoch_mjd": "59069",
"epoch_cal": "20200808",
"equinox": "J2000",
"e": "0.939420568",
"a": "1.390541481",
"q": "0.084238213",
"i": "37.55936253",
"om": "165.2569181",
"w": "116.4217142",
"ma": "0.699590126",
"ad": "2.69684475",
"n": "0.601074288",
"tp": "2459068.336",
"tp_cal": "20200806.84",
"per": "598.927632",
"per_y": "1.639774489",
"moid": "0.335478",
"moid_ld": "130.5579733",
"moid_jup": "3.29387",
"t_jup": "4.023",
"sigma_e": "5.0775",
"sigma_a": "113.92",
"sigma_q": "0.28571",
"sigma_i": "93.792",
"sigma_om": "70.845",
"sigma_w": "359.42",
"sigma_ma": "83.269",
"sigma_ad": "220.95",
"sigma_n": "73.867",
"sigma_tp": "4.8578",
"sigma_per": "73603",
"class": "ETc",
"producer": "Davide Farnocchia",
"data_arc": "",
"first_obs": "8/7/2020",
"last_obs": "8/7/2020",
"n_obs_used": "29",
"n_del_obs_used": "",
"n_dop_obs_used": "",
"condition_code": "9",
"rms": "0.024266",
"two_body": "",
"A1": "",
"A2": "",
"A3": "",
"DT": ""
}
}
Each nested dict uses the pdes as key.
I also have a list of nested dicts. The first three nested dicts (of about 30,000) within the list are:
cad_list
[{"des": "2003 JC17", "orbit_id": "18", "jd": "2488068.556306376", "cd": "2099-Dec-31 01:21", "dist": "0.338023789278089", "dist_min": "0.337895151250112", "dist_max": "0.338152711373013", "v_rel": "22.5355709843525", "v_inf": "22.5352212003886", "t_sigma_f": "00:58", "h": "17.8"}
{"des": "2017 WS12", "orbit_id": "7", "jd": "2488068.734163492", "cd": "2099-Dec-31 05:37", "dist": "0.141595130202053", "dist_min": "0.129742488499914", "dist_max": "0.156296976341895", "v_rel": "7.51471164318556", "v_inf": "7.51220712553632", "t_sigma_f": "3_07:42", "h": "23.2"}
{"des": "2010 XB24", "orbit_id": "19", "jd": "2488069.369087819", "cd": "2099-Dec-31 20:51", "dist": "0.126306889299689", "dist_min": "0.125428658725108", "dist_max": "0.127185695415594", "v_rel": "16.6758717193855", "v_inf": "16.6746066532063", "t_sigma_f": "01:03", "h": "21.8"}]
What I want to do is create a new dict that holds all of the information from neo_dict. Then, if the value in des for each nested dict within cad_list matches any of the keys within neo_dict, I want to add that nested dict from cad_list onto the nested dict within neo_dict.
So for instance:
neo_dict
{
"2020 M3": {
"id": "dK20M030",
"spkid": "1003699",
"full_name": " C/2020 M3 (ATLAS)",}}
cad_list
[{"des": "2020 M3", "orbit_id": "18", "jd": "2488068.556306376"}]
new_dict
{
"2020 M3": {
"id": "dK20M030",
"spkid": "1003699",
"full_name": " C/2020 M3 (ATLAS)",
"orbit_id": "18",
"jd": "2488068.556306376",
}}
I tried to make it as simple as possible. Please let me know if you have any questions.
**
UPDATE:
**
I am taking it step by step. Right now, I am trying to access the 'des' key within cad_list, but I am getting a TypeError.
>>> print(cad_list[0]['des'])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\xxx", line 110, in __init__
print(cad_list[0]['des'])
TypeError: string indices must be integers
What you could do is go through the values of of your cad_list
for stuff in cad_list:
then check if the "des" is in neo_dict
if stuff['des'] in neo_dict:
Then if it matches you can create a new dict from the info you get. I am not sure how you want this new dict, in a list or another dict so wasn't sure how to code that portion out.
I figured out the problem I was having. When I created cad_list by appending each dict item, I appended the dictionary incorrectly (I used json.dumps(cad_dict)). So, when I was trying to access the keys in the dict, the nested dict was actually an str. But, when using the print(cad_dict) function, it printed it out looking like a nested dict.
I ended up using type(cad_dict[0]) which told me it was an str.
Related
Sample
template = {
"Table": [
{
"level": 2,
"value": {
"element Name": "",
"add Row": "False",
"cost Type": "",
"flag": "flag",
"cost": "",
"actions": "True"
},
"RHS": {},
"children": [
{
"level": 3,
"value": {
"element Name": "",
"add Row": "False",
"cost Type": "",
"cost": "",
"actions": "True"
},
"RHS": {},
"children": []
}
]
}
]
}
Considering the above dictionary, I want to append to the last "children" and every time loop runs it should append to the children created in previous iteration.
Loop 1:
"children":{
"level": 4,
"value": {"element Name": "",
"add Row": "False",
"cost Type": "",
"cost": "",
"actions": "True"},
"RHS": {},
"children":
[]
}
Loop 2:
iteration 2
"children":{
"level": 5,
"value": {"element Name": "",
"add Row": "False",
"cost Type": "",
"cost": "",
"actions": "True"},
"RHS": {},
"children":
[]
}
and so on.
My code is:
Python code for loop
for _ in range(sublevels):
number = number + 1
child = {"level": sublevels + 2,
"value": {"element Name": "", "add Row": False,
"cost Type": "", "cost": "",
"actions": True}, "RHS": {}, "children": []}
template['Table'][0]['children'].append(child)
Output:
After iteration, the JSON should look like below
{
"Table": [
{
"level": 2,
"value": {
"element Name": "",
"add Row": "False",
"cost Type": "",
"flag": "flag",
"cost": "",
"actions": "True"
},
"RHS": {},
"children": [
{
"level": 3,
"value": {
"element Name": "",
"add Row": "False",
"cost Type": "",
"cost": "",
"actions": "True"
},
"RHS": {},
"children": [
[
{
"level": 4,
"value": {
"element Name": "",
"add Row": "False",
"cost Type": "",
"cost": "",
"actions": "True"
},
"RHS": {},
"children": [
[
{
"level": 5,
"value": {
"element Name": "",
"add Row": "False",
"cost Type": "",
"cost": "",
"actions": "True"
},
"RHS": {},
"children": []
}
]
]
}
]
]
}
]
}
]
}
Iteration 1: template['Table'][0]['children']
Iteration 2: template['Table'][0]['children'][0]['children']
Iteration 3: template['Table'][0]['children'][0]['children'][0]['children']
import json
template = {"Table": []}
sublevels = 5
for _ in range(sublevels):
#number = number + 1
child = {"level": _ + 2,
"value": {"element Name": "", "add Row": False,
"cost Type": "", "cost": "",
"actions": True}, "RHS": {}, "children": []}
cur_path = "[0]['children']"*_
if _ == 0:
template['Table'].append(child)
else:
exec(f"template['Table']{cur_path}.append(child)")
print(json.dumps(template, indent = 2))
Not the prettiest way, you should avoid using exec, but I was trying to call a JSON path from a dict, and it wasn't working so I used exec.
This works well and nests it tho..
Output I got from running this code:
{
"Table": [
{
"level": 2,
"value": {
"element Name": "",
"add Row": false,
"cost Type": "",
"cost": "",
"actions": true
},
"RHS": {},
"children": [
{
"level": 3,
"value": {
"element Name": "",
"add Row": false,
"cost Type": "",
"cost": "",
"actions": true
},
"RHS": {},
"children": [
{
"level": 4,
"value": {
"element Name": "",
"add Row": false,
"cost Type": "",
"cost": "",
"actions": true
},
"RHS": {},
"children": [
{
"level": 5,
"value": {
"element Name": "",
"add Row": false,
"cost Type": "",
"cost": "",
"actions": true
},
"RHS": {},
"children": [
{
"level": 6,
"value": {
"element Name": "",
"add Row": false,
"cost Type": "",
"cost": "",
"actions": true
},
"RHS": {},
"children": []
}
]
}
]
}
]
}
]
}
]
}
I have a JSON file below and im trying to get the "active_cases" result, but anytime i try to access it using ["rows"] i get an error, anyone with any idea how to get it?
{
"data": {
"paginationMeta": {
"currentPage": 1,
"currentPageSize": 2,
"totalPages": 1,
"totalRecords": 2
},
"last_update": "May, 08 2020, 16:29, UTC",
"rows": [
{
"country": "Netherlands",
"country_abbreviation": "NL",
"total_cases": "42,093",
"new_cases": "319",
"total_deaths": "5,359",
"new_deaths": "71",
"total_recovered": "N/A",
"active_cases": "36,484",
"serious_critical": "564",
"cases_per_mill_pop": "2,457.0",
"flag": "https://www.worldometers.info/img/flags/nl-flag.gif"
},
{
"country": "Caribbean Netherlands",
"country_abbreviation": "",
"total_cases": "6",
"new_cases": "0",
"total_deaths": "0",
"new_deaths": "0",
"total_recovered": "0",
"active_cases": "6",
"serious_critical": "0",
"cases_per_mill_pop": "229.0",
"flag": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/International_Flag_of_Planet_Earth.svg/800px-International_Flag_of_Planet_Earth.svg.png"
}
]
},
"status": "success"
}
active_cases = []
for row in dict['data']['rows']:
active_cases.append(row['active_cases'])
You first load the json with the following:
import json
d = json.loads("""{"data": {"paginationMeta": {"currentPage": 1, "currentPageSize": 2, "totalPages": 1, "totalRecords": 2}, "last_update": "May, 08 2020, 16:29, UTC", "rows": [{"country": "Netherlands", "country_abbreviation": "NL", "total_cases": "42,093", "new_cases": "319", "total_deaths": "5,359", "new_deaths": "71", "total_recovered": "N/A", "active_cases": "36,484", "serious_critical": "564", "cases_per_mill_pop": "2,457.0", "flag": "https://www.worldometers.info/img/flags/nl-flag.gif"}, {"country": "Caribbean Netherlands", "country_abbreviation": "", "total_cases": "6", "new_cases": "0", "total_deaths": "0", "new_deaths": "0", "total_recovered": "0", "active_cases": "6", "serious_critical": "0", "cases_per_mill_pop": "229.0", "flag": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/International_Flag_of_Planet_Earth.svg/800px-International_Flag_of_Planet_Earth.svg.png"}]}, "status": "success"}""")
Then you can filter the json in the following way:
result = [item['active_cases'] for item in d['data']['rows']]
The active cases are in list that in the rows inside data so you can do
import json
json_obj = """{"data": {"paginationMeta": {"currentPage": 1, "currentPageSize": 2, "totalPages": 1, "totalRecords": 2}, "last_update": "May, 08 2020, 16:29, UTC", "rows": [{"country": "Netherlands", "country_abbreviation": "NL", "total_cases": "42,093", "new_cases": "319", "total_deaths": "5,359", "new_deaths": "71", "total_recovered": "N/A", "active_cases": "36,484", "serious_critical": "564", "cases_per_mill_pop": "2,457.0", "flag": "https://www.worldometers.info/img/flags/nl-flag.gif"}, {"country": "Caribbean Netherlands", "country_abbreviation": "", "total_cases": "6", "new_cases": "0", "total_deaths": "0", "new_deaths": "0", "total_recovered": "0", "active_cases": "6", "serious_critical": "0", "cases_per_mill_pop": "229.0", "flag": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/International_Flag_of_Planet_Earth.svg/800px-International_Flag_of_Planet_Earth.svg.png"}]}, "status": "success"}"""
print([i["active_cases"] for i in json.loads(json_obj)["data"]["rows"]])
That will print you the 2 numbers of active cases
My code was working as of yesterday but today I ran the script and started getting this error saying I can't use strings to access my JSON dictionary anymore. As I understand it, I am already iterating through my dictionary entries and it is valid JSON so I should be able to access information in it like a dictionary with string values instead of just the column number:
S:\Scripts\ZACH\DB MERGE>python jsonMerge.py
Beginning Merge...
Traceback (most recent call last):
File "jsonMerge.py", line 42, in <module>
if intResult['SCH_NAME'] == extResult['SCH_NAME'] and intResult['SCH_CITY']
== extResult['SCH_CITY'] :
TypeError: list indices must be integers or slices, not str
I'm merging 2 validated JSON files like this:
[{"SCH_ID": "13554", "SCH_NAME": "100 Mile House Elementary", "SCH_ADDR": "Box 460, 145 North Birch", "SCH_CITY": "100 Mile House", "SCH_PROV": "BC", "SCH_PCODE": "V0K 2E0", "SCH_PHONE": "(250)395-2258", "SCH_FAX": "(250)395-3621", "SCH_DIST": "1027", "SCH_TYPE": "E", "SCH_P_REP": "FB", "SCH_G_REP": "", "SCH_P_COM": "LOC", "SCH_G_COM": "", "SCH_REBT": "10", "SCH_REBT2": "0", "SCH_P_CID": "23", "SCH_G_CID": "0", "SCH_P_CCD": "SR", "SCH_G_CCD": "", "DATE1": "", "DATE2": "", "PLAN1": "20G", "PLAN2": "2GR", "LNOPST": "FALSE"},{"SCH_ID": "16101", "SCH_NAME": "1 Step Ahead Preschool", "SCH_ADDR": "1340 Kingfisher Ave.", "SCH_CITY": "Kitimat", "SCH_PROV": "BC", "SCH_PCODE": "V8C 1G6", "SCH_PHONE": "(250)632-2288", "SCH_FAX": "", "SCH_DIST": "", "SCH_TYPE": "E", "SCH_P_REP": "FB", "SCH_G_REP": "", "SCH_P_COM": "P", "SCH_G_COM": "", "SCH_REBT": "0", "SCH_REBT2": "0", "SCH_P_CID": "23", "SCH_G_CID": "0", "SCH_P_CCD": "SR", "SCH_G_CCD": "", "DATE1": "", "DATE2": "", "PLAN1": "200", "PLAN2": "0", "LNOPST": "FALSE"},{"SCH_ID": "16736", "SCH_NAME": "1st Step Montessori", "SCH_ADDR": "8884 Russell Drive", "SCH_CITY": "Delta", "SCH_PROV": "BC", "SCH_PCODE": "V4C 4P8", "SCH_PHONE": "(604)417-3290", "SCH_FAX": "", "SCH_DIST": "1037", "SCH_TYPE": "E", "SCH_P_REP": "GF", "SCH_G_REP": "", "SCH_P_COM": "MWS", "SCH_G_COM": "", "SCH_REBT": "10", "SCH_REBT2": "0", "SCH_P_CID": "18", "SCH_G_CID": "0", "SCH_P_CCD": "SB", "SCH_G_CCD": "", "DATE1": "", "DATE2": "", "PLAN1": "20G", "PLAN2": "0", "LNOPST": "FALSE"},{"SCH_ID": "1959", "SCH_NAME": "150 Mile Elementary", "SCH_ADDR": "Box 259, 3081 Hwy. 97", "SCH_CITY": "150 Mile House", "SCH_PROV": "BC", "SCH_PCODE": "V0K 2G0", "SCH_PHONE": "(250)296-3356", "SCH_FAX": "(250)296-3291", "SCH_DIST": "1027", "SCH_TYPE": "E", "SCH_P_REP": "FB", "SCH_G_REP": "", "SCH_P_COM": "MWS", "SCH_G_COM": "", "SCH_REBT": "10", "SCH_REBT2": "0", "SCH_P_CID": "23", "SCH_G_CID": "0", "SCH_P_CCD": "SR", "SCH_G_CCD": "", "DATE1": "9/12/2018", "DATE2": "10/30/2018", "PLAN1": "2GS", "PLAN2": "2GR", "LNOPST": "FALSE"}]
and:
[{"District Number": "82", "School Code": "8297024", "SCH_NAME": "Na Aksa Gyilak'yoo", "SCH_ADDR": "PO Box 544 STN Main", "SCH_CITY": "Terrace", "SCH_PROV": "BC", "SCH_PCODE": "V8G 4B5", "Principal Title": "Mrs", "Principal First name": "Colleen", "Principal Last Name": "Austin", "School Type": "Standard School", "Grade Range": "K-12", "School Category": "Independent School", "Funding Group(s)": "2", "NLC: Early Learning": "no", "NLC: Afterschool": "no", "NLC: Cont. Ed.": "no", "NLC: Seniors": "no", "NLC: Comm. Sport": "no", "NLC: Comm. Use": "no", "NLC: Integr. Svcs.": "no", "SCH_PHONE": "(250)615-2844", "SCH_FAX": "(250)615-2833", "Email": "kalumteacher#gmail.com", "Enrolment Total": "80", "Enrolment As Of": "September 30 2018", "KH Enrolment": "1", "KF Enrolment": "11", "HS Registration": "0", "SU Enrolment": "0", "EU Enrolment": "0", "Grade 1 Enrolment": "2", "Grade 2 Enrolment": "8", "Grade 3 Enrolment": "4", "Grade 4 Enrolment": "5", "Grade 5 Enrolment": "4", "Grade 6 Enrolment": "8", "Grade 7 Enrolment": "4", "Grade 8 Enrolment": "9", "Grade 9 Enrolment": "5", "Grade 10 Enrolment": "11", "Grade 11 Enrolment": "3", "Grade 12 Enrolment": "5"}]
using SCH_NAME and SCH_CITY as the primary keys:
with open(extFile, 'r') as extF:
#Iterate through every entry
for extLine in extF:
hasMatched = False
#load line
extResult = json.loads(extLine)
#print ("Checking: " + intResult['SCH_NAME'] + '\n')
#Set context as the external result to start us off
#contextLine = extResult
with open(intFile, 'r') as intF:
#Iterate through every entry
for intLine in intF:
#Load line
intResult = json.loads(intLine)
#print ("Matching: " + extResult['SCH_NAME'] + '\n')
#Check if rows match
if intResult['SCH_NAME'] == extResult['SCH_NAME'] and intResult['SCH_CITY'] == extResult['SCH_CITY'] :
#We have a match
hasMatched = True
Can anyone help shine a light on what might be going wrong here?
Yea ok looking at the error and the two json files, they're coming in as lists instead of dicts. You may want to figure out why this happened, but you can do a quick workaround like so.
intResult = json.loads(intLine)[0]
note you may need to do the same for extResult
I have an extra last square } in a big json file, I need to remove it by using python :
{
"layers": {
"frame": {
"frame.interface_id": "0",
"frame.encap_type": "127",
"frame.time": "Oct 10, 2017 18:05:51.620568000 Central European Daylight Time",
"frame.offset_shift": "0.000000000",
"frame.time_epoch": "1507651551.620568000",
"frame.time_delta": "0.324011000",
"frame.time_delta_displayed": "0.324011000",
"frame.time_relative": "29.248970000",
"frame.number": "38",
"frame.len": "64",
"frame.cap_len": "64",
"frame.marked": "0",
"frame.ignored": "0",
"frame.protocols": "wpan:6lowpan:ipv6:ipv6.hopopts:udp:data",
"frame.coloring_rule.name": "UDP",
"frame.coloring_rule.string": "udp"
},
"wpan": {
"wpan.frame_length": "66",
"wpan.fcf": "0x0000dc41",
"wpan.fcf_tree": {
"wpan.frame_type": "0x00000001",
"wpan.security": "0",
"wpan.pending": "0",
"wpan.ack_request": "0",
"wpan.pan_id_compression": "1",
"wpan.seqno_suppression": "0",
"wpan.ie_present": "0",
"wpan.dst_addr_mode": "0x00000003",
"wpan.version": "1",
"wpan.src_addr_mode": "0x00000003"
},
"wpan.seq_no": "8",
"wpan.dst_pan": "0x0000abcd",
"wpan.dst64": "00:21:2f:3c:c6:b5:00:01",
"wpan.src64": "00:21:2f:3c:c6:b5:00:7e",
"wpan.fcs_ok": "1"
},
"6lowpan": {
"IPHC Header": {
"6lowpan.pattern": "0x00000003",
"6lowpan.iphc.tf": "0x00000003",
"6lowpan.iphc.nh": "0",
"6lowpan.iphc.hlim": "0x00000002",
"6lowpan.iphc.cid": "1",
"6lowpan.iphc.sac": "1",
"6lowpan.iphc.sam": "0x00000003",
"6lowpan.iphc.m": "0",
"6lowpan.iphc.dac": "1",
"6lowpan.iphc.dam": "0x00000003",
"6lowpan.iphc.sci": "0x00000000",
"6lowpan.iphc.dci": "0x00000000"
},
"6lowpan.next": "0x00000000",
"6lowpan.src": "::221:2f3c:c6b5:7e",
"6lowpan.dst": "::221:2f3c:c6b5:1"
},
"ipv6": {
"ipv6.version": "6",
"ip.version": "6",
"ipv6.tclass": "0x00000000",
"ipv6.tclass_tree": {
"ipv6.tclass.dscp": "0",
"ipv6.tclass.ecn": "0"
},
"ipv6.flow": "0x00000000",
"ipv6.plen": "39",
"ipv6.nxt": "0",
"ipv6.hlim": "64",
"ipv6.src": "::221:2f3c:c6b5:7e",
"ipv6.addr": "::221:2f3c:c6b5:7e",
"ipv6.src_host": "::221:2f3c:c6b5:7e",
"ipv6.host": "::221:2f3c:c6b5:7e",
"ipv6.dst": "::221:2f3c:c6b5:1",
"ipv6.addr": "::221:2f3c:c6b5:1",
"ipv6.dst_host": "::221:2f3c:c6b5:1",
"ipv6.host": "::221:2f3c:c6b5:1",
"Source GeoIP: Unknown": "",
"Destination GeoIP: Unknown": "",
"ipv6.hopopts": {
"ipv6.hopopts.nxt": "17",
"ipv6.hopopts.len": "0",
"ipv6.hopopts.len_oct": "8",
"ipv6.opt": {
"ipv6.opt.type": "99",
"ipv6.opt.type_tree": {
"ipv6.opt.type.action": "1",
"ipv6.opt.type.change": "1",
"ipv6.opt.type.rest": "0x00000003"
},
"ipv6.opt.length": "4",
"ipv6.opt.rpl.flag": "0x00000000",
"ipv6.opt.rpl.flag_tree": {
"ipv6.opt.rpl.flag.o": "0",
"ipv6.opt.rpl.flag.r": "0",
"ipv6.opt.rpl.flag.f": "0",
"ipv6.opt.rpl.flag.rsv": "0x00000000"
},
"ipv6.opt.rpl.instance_id": "0x0000001e",
"ipv6.opt.rpl.sender_rank": "0x00000200"
}
}
},
"udp": {
"udp.srcport": "30002",
"udp.dstport": "3000",
"udp.port": "30002",
"udp.port": "3000",
"udp.length": "31",
"udp.checksum": "0x00007ca5",
"udp.checksum.status": "2",
"udp.stream": "17"
},
"data": {
"data.data": "2f:14:02:15:20:ed:1a:05:02:40:29:5c:ab:41:cc:23:c7:42:10:d8:eb:41:45",
"data.len": "23"
}
}
}
}
,
How could I remove it please?
I would be very grateful if you help me please?
First thing first: having one extra closing brace means this is not valid json, so the best thing to do would be to cure the problem at the source. If this comes verbatim from some api then contact the tech staff, if this comes from your own code then fix it where this extra brace is introduced.
This being said, assuming your json is stored as a string data, then removing the last closing brace is as simple as
data = data.strip().rstrip("}")
If this is part of an automated process and you only sometimes have this extraneaous brace, you can test before cleaning up:
if data.count("}") > data.count("{"):
data = data.strip().rstrip("}")
I have this json object in ajax_data variable
{
"columns[0][data]": "0",
"columns[1][name]": "",
"columns[5][searchable]": "true",
"columns[5][name]": "",
"columns[4][search][regex]": "false",
"order[0][dir]": "asc",
"length": "10",
}
I have converted it using json.loads() function like.
ajax_data = json.loads(ajax_data)
I want to get the value if "order[0][dir]" and "columns[0][data]" but if i print it using
ajax_data['order'][0]['dir]
its giving error :
KeyError at /admin/help
'order'
But same code works if i access it for length key then it works.
The keys you have used are actually not a good way of implementation.
{
"columns[0][data]": "0",
"columns[1][name]": "",
"columns[5][searchable]": "true",
"columns[5][name]": "",
"columns[4][search][regex]": "false",
"order[0][dir]": "asc",
"length": "10",
}
Instead of this you should hav gone for
{
"columns": [
{"data": "0", "name": "", "searchable": "true", "name": "", "search": {
"regex": "false"}
},
{"data": "0", "name": "", "searchable": "true", "name": ""," search": {
"regex": "false"}},
{"data": "0", "name": "", "searchable": "true", "name": "", "search": {
"regex": "false"}},
{"data": "0", "name": "", "searchable": "true", "name": "", "search": {
"regex": "false"}},
{"data": "0", "name": "", "searchable": "true", "name": "", "search": {
"regex": "false"}},
{"data": "0", "name": "", "searchable": "true", "name": "", "search": {
"regex": "false"}},
],
"order": [
{"dir": "asc"}
],
"length": "10"
}
In this case ajax_data['order'][0]['dir] will result in value "asc"
For your current implementation the key is "order[0][dir]"
That is go for
ajax_data["order[0][dir]"]
Hope you understood the issue.
Structuring of json is very important when dealing with APIs. Try to restructure your json which will help for future too.
That's because length is a key in that json object, and order is not. The key names are the entire strings inside the quotes: columns[0][data], order[0][dir], etc.
Those are unusual key names, but perfectly valid.