1

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!

2
  • 1
    Your json is invalid, there are two trailling commas after "code": "201900001" and "code": "201900003". See jsonformatter.curiousconcept.com Commented Nov 1, 2019 at 11:01
  • @StaticX yes and thanks, let me correct it Commented Nov 1, 2019 at 12:29

1 Answer 1

2

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'].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.