如何使用 jsonpath + Python 获取某些元素?

How to get certain elements using jsonpath + Python?

我有一个名为source.json的文件,内容是

{
    "msg": "OK",
    "result": {
        "data": {
            "articles": [
                {
                    "idtArticle": "CF00002",
                    "promotionService": {
                        "bundleSales": [
                            {
                                "code": "201900001"
                            },
                            {
                                "code": "201900002"
                            }
                        ]
                    }
                },
                {
                    "idtArticle": "CF00003",
                    "promotionService": {
                        "bundleSales": [
                            {
                                "code": "201900001"
                            },
                            {
                                "code": "201900003"
                            }
                        ]
                    }
                }
            ]
        }
    }
}

我有 Python 代码如下:

import json
import jsonpath

json_source = 'source.json'

with open(json_source, encoding='utf-8') as f:
    root = json.loads(f.read())

if __name__ == "__main__":
    result = jsonpath.jsonpath(root, """$..articles[?(@.idtArticle == "CF00002")]""")
    print(result)

代码有效,我可以获取idtArticle为CF00002的文章,但是如何获取code(或2个code之一)为201900001的文章列表?

感谢所有帮助!

jsonpath 不支持投影 所以我会简单地做你想做的 python.

import json

json_source = 'source.json'
with open(json_source, encoding='utf-8') as f:
    root = json.loads(f.read())

if __name__ == "__main__":
    articles = root['result']['data']['articles']
    result = []
    for article in articles:
        bundleSales = article['promotionService']['bundleSales']
        for bundleSale in bundleSales:
            if bundleSale['code'] == "201900001":
                result.append(article['idtArticle'])
    print(result)

您可以使用扩展示例进行测试:

{
    "msg": "OK",
    "result": {
        "data": {
            "articles": [
                {
                    "idtArticle": "CF00002",
                    "promotionService": {
                        "bundleSales": [
                            {
                                "code": "201900001"
                            },
                            {
                                "code": "201900002"
                            }
                        ]
                    }
                },
                {
                    "idtArticle": "CF00003",
                    "promotionService": {
                        "bundleSales": [
                            {
                                "code": "201900001"
                            },
                            {
                                "code": "201900003"
                            }
                        ]
                    }
                },
                {
                    "idtArticle": "CF00004",
                    "promotionService": {
                        "bundleSales": [
                            {
                                "code": "201900002"
                            },
                            {
                                "code": "201900003"
                            }
                        ]
                    }
                }
            ]
        }
    }
}

它打印 ['CF00002', 'CF00003'].