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

2024-05-23 14:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件名为源.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的文章,但是如何得到代码(或两个代码之一)为201900001的文章列表?你知道吗

感谢所有的帮助!你知道吗


Tags: 代码importjsonsource内容文件名文章code
1条回答
网友
1楼 · 发布于 2024-05-23 14:21:11

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']。你知道吗

相关问题 更多 >