Jsonpath syntax to extract specific parent keys with specific child keys

Is it possible to filter json by jsonpath query to get specific keys of parents AND specific keys of childs?

Here is the json:

{
    "data": {
        "paymentProviders": [
            {
                "currency": "USD",
                "supported": true, 
                "providers": [
                    {
                        "fields": [],
                        "key": "paypal",
                        "maintenance": false,
                        "settings": [
                            {
                                "max": 10000,
                                "min": 100
                            }
                        ],
                        "withdrawalFields": []
                    },
                    {
                        "fields": [],
                        "key": "moneta",
                        "maintenance": false,
                        "settings": [
                            {
                                "key": "deposits",
                                "max": 10000,
                                "min": 100
                            }
                        ],
                        "withdrawalFields": []
                    }
                ]
            },
            {
                "currency": "GBP",
                "supported": true, 
                "providers": [
                    {
                        "fields": [],
                        "key": "directTransfer",
                        "maintenance": false,
                        "settings": [
                            {
                                "key": "deposits",
                                "max": 0,
                                "min": 100
                            }
                        ],
                        "withdrawalFields": []
                    }
                ]
            },
            {
                "currency": "EUR",
                "supported": true, 
                "providers": [
                    {
                        "fields": [],
                        "key": "paypal",
                        "maintenance": false,
                        "settings": [
                            {
                                "key": "deposits",
                                "max": 10000,
                                "min": 100
                            }
                        ],
                        "withdrawalFields": []
                    }
                ]
            }
        ]
    }
}

I want to get only 'currency', 'providers.key' and 'providers.maintenance' from it, like this:

{
    "data": {
        "paymentProviders": [
            {
                "currency": "USD",
                "providers": [
                    {
                        "key": "paypal",
                        "maintenance": false,
                    },
                    {
                        "key": "moneta",
                        "maintenance": false,
                    }
                ]
            },
            {
                "currency": "GBP",
                "providers": [
                    {
                        "key": "directTransfer",
                        "maintenance": false,
                    }
                ]
            },
            {
                "currency": "EUR",
                "providers": [
                    {
                        "key": "paypal",
                        "maintenance": false,
                    }
                ]
            }
        ]
    }
}

If I use something like

$..['key', 'maintenance']

I'll lose that 'currency' property.

Basically, I want to use 'Union operator' pattern inside another 'Union operator' pattern, idea is this:

$..["currency","providers.['key', 'maintenance']"]

Upvotes: 0

Views: 722

Answers (1)

Daniel
Daniel

Reputation: 763

Try

$..['currency','key','maintenance']

You should get

[
    "USD",
    "paypal",
    false,
    "moneta",
    false,
    "deposits",
    "GBP",
    "directTransfer",
    false,
    "deposits",
    "EUR",
    "paypal",
    false,
    "deposits"
]

Compare with Goessner

Note that the jsonpath union operator will only give you a list of values, without the contextual elements that you have in your desired results.

Upvotes: 3

Related Questions