2

I have a data structure like this and want to extract nested data using conditional expressions.

from jsonpath_ng.ext import parse

ex = {
    "data": {
        "a": {
            "id": 1,
            "selected": False,
            "content": {
                "text": "foo"
            }
        },
        "b": {
            "id": 2,
            "selected": True,
            "content": {
                "text": "bar"
            }
        },
        "c": {
            "id": 3,
            "selected": True,
            "content": {
                "text": "foobar"
            }
        }
    }
}

for m in parse("data[?selected==true].content.text").find(ex):
    print(m.full_path, m.value)

What I get is

data.[1].content.text bar
data.[2].content.text foobar

But what I expected and need is

data.b.content.text bar
data.c.content.text foobar

How can I do the conditional match and also get the information if "a", "b", or "c" was selected? It is okay to use a different library than jsonpath-ng. But I don't want to code a function myself that does a post processing with the index in the path or iterate over .keys() and do all that processing that jsonpath-ng is supposed to do.

goerlitz
  • 505
  • 4
  • 15
  • I am also having the same issue. For some reason it gives me the index of a particular object, not the key. It does not seem to be useful given that I cannot use the subscript operator with dict.keys() – Reid-o Sep 26 '22 at 13:00

0 Answers0