Home > other >  How to merge/append two json array list in python in a specific way
How to merge/append two json array list in python in a specific way

Time:03-16

I have two list of Json array output,

x =

[
    {
        "id": "5019",
        "tiClass": "Device",
        "tiSubclass": "Virtual Machine",
        "cmbStatus": "Planned",
        "cmbUPosition": "",
        "Subnet Mask": null
    },
    {
        "id": "5020",
        "tiClass": "Device",
        "tiSubclass": "Virtual Machine",
        "cmbStatus": "Planned",
        "cmbUPosition": "",
        "cmbType": "Virtual LoadBalancer",
        "Subnet Mask": null
    }
]

y =

[
    {
        "dataports": [
            {
                "itemId": 5019,
                "portId": 12614,
                "used": false,
                "reserved": false,
                "type": "Virtual"
            },
            {
                "itemId": 5019,
                "portId": 12615,
                "used": false,
                "reserved": false,
                "type": "Virtual"
            }            
            
        ]
    },
    {
        "dataports": [
            {
                "itemId": 5020,
                "portId": 12615,
                "used": false,
                "reserved": false,
                "type": "Virtual"
            }
        ]
    }
]

the way i want the result is:

result =

[
    {
        "id": "5019",
        "tiClass": "Device",
        "tiSubclass": "Virtual Machine",
        "cmbStatus": "Planned",
        "cmbUPosition": "",
        "Subnet Mask": null,
        "dataports": [
            {
                "itemId": 5019,
                "portId": 12614,
                "used": false,
                "reserved": false,
                "type": "Virtual"
            },
            {
                "itemId": 5019,
                "portId": 12615,
                "used": false,
                "reserved": false,
                "type": "Virtual"
            }            
            
        ]
    },
    {
        "id": "5020",
        "tiClass": "Device",
        "tiSubclass": "Virtual Machine",
        "cmbStatus": "Planned",
        "cmbUPosition": "",
        "cmbType": "Virtual LoadBalancer",
        "Subnet Mask": null,
        "dataports": [
            {
                "itemId": 5020,
                "portId": 12615,
                "used": false,
                "reserved": false,
                "type": "Virtual"
            }
        ]
    }
]

i have tried result = x y but the result output is not what i am looking for. i have searched SO for any references/examples that will suit to my use case but couldn't find. Can anyone suggest the right way to get above result output (list Y should be appended/added to list x )

CodePudding user response:

With your examples, you need to merge each sub dict in the list like so:

x=json.load(open("/tmp/x"))
y=json.load(open("/tmp/y"))
your_result=json.load(open("/tmp/your_result"))

n=[{**x_, **y_} for x_,y_ in zip(x,y)]

>>> n==your_result
True
  • Related