Search code examples
pythonjsonpython-3.xjsonpath

How to get certain elements using jsonpath + Python?


I have a file named source.json, the content is

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

I have Python code as following:

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)

The code works and I can get the article whose idtArticle is CF00002, but how to get the article list whose code(or one of the 2 codes) is 201900001?

Appreciate all the helps!


Solution

  • jsonpath does not support projections so I would do what you want in simple 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)
    

    You can test it with an extended example:

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

    It prints ['CF00002', 'CF00003'].