Jsonpath语法可提取具有特定子键的特定父键

时间:2018-06-26 09:46:45

标签: json jsonpath

是否可以通过jsonpath查询过滤json以获取父母的特定键和孩子的特定键?

这是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": []
                    }
                ]
            }
        ]
    }
}

我只想从中获取“ currency”,“ providers.key”和“ providers.maintenance”,就像这样:

{
    "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,
                    }
                ]
            }
        ]
    }
}

如果我使用

  

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

我会丢失“ currency”属性。

基本上,我想在另一个“联合运算符”模式中使用“联合运算符”模式,想法是:

  

$ .. [“ currency”,“提供者。['key','maintenance']”]

1 个答案:

答案 0 :(得分:0)

尝试

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

您应该得到

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

Goessner

进行比较

请注意,jsonpath联合运算符将只为您提供一个值列表,而没有您想要的结果中包含的上下文元素。