skip to Main Content

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


  1. you can look at this code :

    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"
            
        },
    ]
    }]
    
    
    
    
    
    first_dict = dataList[0]
    item_list_one = first_dict.pop("item_list_one")
    item_list_two = first_dict.pop("item_list_two")
    first_dict.update({"combined_list" : item_list_one + item_list_two})
    
    
    print(first_dict)
    
    Login or Signup to reply.
  2. 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:

    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" }, ] }]
    
    for item in dataList:
        combined_list = item.get("item_list_one", []) + item.get("item_list_two", [])
        item["combined_list"] = combined_list
        del item["item_list_one"]
        del item["item_list_two"]
    
    print(dataList)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search