首页 > 解决方案 > 如何使用 jmespath 构造路径以获取值

问题描述

我在使用jmespath.search().

只是为了把它放在上下文中,我正在将我的请求中的所有信息下载到一个 CSV 文件中。然后我将其作为 JSON 上传并使用 JMESPath,我希望获取这些值。

我想去#value哪里'_instrumentIdScheme': 'mhi:MHILIST'

json固定:

[
    {
        "_fpmlVersion": "5-6",
        "header": {
            "messageType": "PrevDayCloseBond",
            "sendTo": [
                {
                    "#value": "Anvil"
                }
            ],
            "creationTimestamp": "2021-09-28T06:00:00.000Z"
        },
        "m:asOfDate": {
            "#value": "2021-09-28T00:00:00.000Z"
        },
        "_xmlns": "http://www.fpml.org/FpML-5/reporting",
        "_xmlns:m": "urn:com.mizuho.bdm",
        "_xmlns:mhi": "urn:com.mizuho.bdm.mhi",
        "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "_xsi:schemaLocation": "http://www.fpml.org/FpML-5/reporting http://svc-bdmentity01p:8080/schema/7.2.0/com/mizuho/bdm/fpml/fpml-5-6-reporting.xsd urn:com.mizuho.bdm http://svc-bdmentity01p:8080/schema/7.2.0/com/mizuho/bdm/fpml/mizuho-fpml.xsd urn:com.mizuho.bdm.mhi http://svc-bdmentity01p:8080/schema/7.2.0/com/mizuho/bdm/mhi/fpml/mhi-fpml.xsd",
        "m:assetPricing": [
            {
                "m:pricingSource": [
                    {
                        "#value": "LON-XEN-BBG"
                    },
                    {
                        "#value": "BGN",
                        "_pricingSourceScheme": "mizuho:bloomberg-source"
                    }
                ],
                "m:instrumentId": [
                    {
                        "#value": "100001380992",
                        "_instrumentIdScheme": "mhi:MHILIST"
                    },
                    {
                        "#value": "100001380992",
                        "_instrumentIdScheme": "mhsa:instrument-id"
                    }
                ],
                "m:currency": {
                    "#value": "USD"
                },
                "m:price": [
                    {
                        "value": 140.78125,
                        "measureType": {
                            "#value": "Bid Price",
                            "_assetMeasureScheme": "mizuho:price-type"
                        }
                    },
                    {
                        "value": 140.875,
                        "measureType": {
                            "#value": "Mid Price",
                            "_assetMeasureScheme": "mizuho:price-type"
                        }
                    },
                    {
                        "value": 140.96875,
                        "measureType": {
                            "#value": "Offer Price",
                            "_assetMeasureScheme": "mizuho:price-type"
                        }
                    }
                ]
            }
        ],
        "m:pricingDate": "2021-09-28T00:00:00.000Z"
    }
]

标签: pythonjsonjmespath

解决方案


  1. 用双引号替换所有单引号

  2. 选择所有带有条件的#value:


def flatten(container):
    for i in container:
        if isinstance(i, (list,tuple)):
            for j in flatten(i):
                yield j
        else:
            yield i


str = """
[
    {
        "_fpmlVersion": "5-6",
        "header": {
            "messageType": "PrevDayCloseBond",
            "sendTo": [
                {
                    "#value": "Anvil"
                }
            ],
            "creationTimestamp": "2021-09-28T06:00:00.000Z"
        },
        "m:asOfDate": {
            "#value": "2021-09-28T00:00:00.000Z"
        },
        "_xmlns": "http://www.fpml.org/FpML-5/reporting",
        "_xmlns:m": "urn:com.mizuho.bdm",
        "_xmlns:mhi": "urn:com.mizuho.bdm.mhi",
        "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "_xsi:schemaLocation": "http://www.fpml.org/FpML-5/reporting http://svc-bdmentity01p:8080/schema/7.2.0/com/mizuho/bdm/fpml/fpml-5-6-reporting.xsd urn:com.mizuho.bdm http://svc-bdmentity01p:8080/schema/7.2.0/com/mizuho/bdm/fpml/mizuho-fpml.xsd urn:com.mizuho.bdm.mhi http://svc-bdmentity01p:8080/schema/7.2.0/com/mizuho/bdm/mhi/fpml/mhi-fpml.xsd",
        "m:assetPricing": [
            {
                "m:pricingSource": [
                    {
                        "#value": "LON-XEN-BBG"
                    },
                    {
                        "#value": "BGN",
                        "_pricingSourceScheme": "mizuho:bloomberg-source"
                    }
                ],
                "m:instrumentId": [
                    {
                        "#value": "100001380992",
                        "_instrumentIdScheme": "mhi:MHILIST"
                    },
                    {
                        "#value": "100001380992",
                        "_instrumentIdScheme": "mhsa:instrument-id"
                    }
                ],
                "m:currency": {
                    "#value": "USD"
                },
                "m:price": [
                    {
                        "value": 140.78125,
                        "measureType": {
                            "#value": "Bid Price",
                            "_assetMeasureScheme": "mizuho:price-type"
                        }
                    },
                    {
                        "value": 140.875,
                        "measureType": {
                            "#value": "Mid Price",
                            "_assetMeasureScheme": "mizuho:price-type"
                        }
                    },
                    {
                        "value": 140.96875,
                        "measureType": {
                            "#value": "Offer Price",
                            "_assetMeasureScheme": "mizuho:price-type"
                        }
                    }
                ]
            }
        ],
        "m:pricingDate": "2021-09-28T00:00:00.000Z"
    }
]
"""
str = str.replace("\n", "").replace("\t", "")
str = json.loads(str)   
#print(str)

valueslist = jmespath.search('[]["m:assetPricing"][][]."m:instrumentId"[?"_instrumentIdScheme" == `mhi:MHILIST`].["#value"]', str)   
#print(valueslist)

values = list(flatten(valueslist))
print(values)

结果:

['100001380992']

推荐阅读