I have an end-point returning this JSON structure:
dataList = [{
"item one" : 1234,
"item two" : "5678",
"item three" : "90",
"item_list_one" : [ {
"first_item_list_item_one" : 1,
"first_item_list_item_two" : "def",
"first_item_list_item_three" : "abc"
},
{
"first_item_list_item_one" : 1,
"first_item_list_item_two" : "def",
"first_item_list_item_three" : "abc"
},
],
"item_list_two" : [{
"second_item_list_item_four" : 2,
"second_item_list_item_five" : "mno",
"second_item_list_item_six" : "pqr"
},
{
"item_list_item_seven" : 1,
"item_list_item_eight" : "def",
"item_list_item_nine" : "abc"
},
]
}]
I would like to combine "item_list_one" and "item_list_two" within their parent.
This gets me inside of the JSON
for index in range(len(dataList)):
for key, value in dataList[index].items():
print(key,value)
But when I try to access "item_list_one" for example, it doesn’t know what that is…
for index in range(len(dataList)):
for key, value in dataList[index].items():
for index in range(len(item_list_one)):
for i in item_list_one.items():
print(i)
I know we can combine dictionaries in Python by doing {dict_1 + dict_2) but I can’t get to that stage for some reason.
I would like it to look like this:
dataList = [{
"item one" : 1234,
"item two" : "5678",
"item three" : "90",
"combined_list" : [ {
{
"first_item_list_item_one" : 1,
"first_item_list_item_two" : "def",
"first_item_list_item_three" : "abc"
},
{
"first_item_list_item_one" : 2,
"first_item_list_item_two" : "ghi",
"first_item_list_item_three" : "pon"
},
{
"second_item_list_item_one" : 4,
"second_item_list_item_two" : "grf",
"second_item_list_item_three" : "bfc"
}, ...
]
}]
2
Answers
you can look at this code :
You want to combine "item_list_one" and "item_list_two" within their parent and form a single list, for that you can iterate through each dictionary in dataList. Please look at the code below: