0

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 )

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

thank you . i got the desired output. But i would like to know the logic behind {**x_, **y_} , ```**`` . Can you please explain. Thanks a ton.
It is the older (but more obscure) way to merge dictionaries. In Python 3.9 you have d | other but versions of Python 3.5+ support {**d, **other} to merge those two dicts into a new dict. Covered in PEP 584 and PEP 448

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.