Home > other >  how to filter json array by a value of a nested key - in python
how to filter json array by a value of a nested key - in python

Time:12-13

I am trying to filter a json with an array that can have several objects but can also be empty.

input:

Where everything is fine, it is not empty and I have my customerKey inside the first level:

    "mainArray": [
        {
            "errorsArray": [],
            "errorCode": "0",
                        "customerKey": "UUID-1"
        },
        {
            "errorsArray": [],
            "errorCode": "0",
                        "customerKey": "UUID-2"
        }
    ]

but when there is an error I get my key inside a nested array of errors:

    "mainArray": [
        {
            "errorsArray": [
                {
                    "customerKey": "UUID-1",
                    "description": "error desc",
                    "errorCode": "1"
                },
                {
                    "customerKey": "UUID-1",
                    "description": "error desc",
                    "errorCode": "2"
                }
            ],
            "errorCode": "1"
        },
        {
            "errorsArray": [],
            "errorCode": "0",
                        "customerKey": "UUID-2"
        }
    ]

output:

I would like to filter my "mainField", leaving only customer with UUID-1.

expected output for when everything is ok:

    "mainArray": [
        {
            "errorsArray": [],
            "errorCode": "0",
                        "customerKey": "UUID-1"
        }
    ]

expected output for when there are errors: but when there is an error I get my key inside a nested array of errors:

    "mainArray": [
        {
            "errorsArray": [
                {
                    "customerKey": "UUID-1",
                    "description": "error desc",
                    "errorCode": "1"
                },
                {
                    "customerKey": "UUID-1",
                    "description": "error desc",
                    "errorCode": "2"
                }
            ],
            "errorCode": "1"
        }
    ]

additional info:

I am mentioning that inside errorsArray I do not except to get different customerKeys. I am also mentioning that changing this input is not an option at this moment.

Thank you :)

What I tried so far is:

the following technique obviously works only for situations where there are no errors:

filtered = [obj for obj in mainArray if (obj.get('customerKey',{}) == 'UUID-1')] if len(mainArray ) != 0 else [{}]

It does not work when there are errors because the customerKey is only inside the inner array.

CodePudding user response:

You can use nested list comprehension for this:

mainArray = [
        {
            "errorsArray": [
                {
                    "customerKey": "UUID-1",
                    "description": "error desc",
                    "errorCode": "1"
                },
                {
                    "customerKey": "UUID-1",
                    "description": "error desc",
                    "errorCode": "2"
                }
            ],
            "errorCode": "1"
        },
        {
            "errorsArray": [],
            "errorCode": "0",
                        "customerKey": "UUID-2"
        }
    ]
    
filtered = [x for x in mainArray if [y for y in x["errorsArray"] if y["customerKey"] == "UUID-1"]]

print(filtered)

the if in 'x' comprehension checks whether the 'y' (errors) list is not empty. The 'y' list checks UUID.

  • Related