adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Jsonpath with multiple array seectors #65

Closed hrushikesh198 closed 6 years ago

hrushikesh198 commented 6 years ago

How to write jsonpath when multiple array selectors are required? In the bellow python example the I want to select {"d": 10, "e": 20} first jsonpath looks correct but does not work. The other ones have side effects.

from objectpath import Tree
a={ 
    "a": [
        {
            "b":1,
            "c": [
                {"d": 10, "e": 20},
                {"d": 30, "e": {"d": 10, "e": 11}},
            ],
            "b2": {
                "c":[
                    {"d":10, "e": 12}
                ]
            }
        },
        {"b": 2}
    ]
}
print list(Tree(a).execute("$.a[@.b is 1].c[@.d is 10]"))
print list(Tree(a).execute("$.a[@.b is 1].c..*[@.d is 10]"))
print list(Tree(a).execute("$.a[@.b is 1]..c[@.d is 10]"))
Output:
[]
[{'e': 20, 'd': 10}, {'e': 11, 'd': 10}]
[{'e': 20, 'd': 10}, {'e': 12, 'd': 10}]
adriank commented 6 years ago

I don't remember exactly why, but $.a[@.b is 1].c returns [[{'e': 20, 'd': 10}, {'e': 11, 'd': 10}]] so the answer is to add [0] after c like this:

print list(Tree(a).execute("$.a[@.b is 1].c[0][@.d is 10]"))

Greetings, Adrian Kalbarczyk

http://kalbarczyk.co

On Wed, Jul 11, 2018 at 12:28 AM hrushikesh198 notifications@github.com wrote:

How to write jsonpath when multiple array selectors are required? In the bellow python example the I want to select { "d": 10, "e": 20} first jsonpath looks correct but does not work. The other ones have side effects.

a={ "a": [ { "b":1, "c": [ { "d": 10, "e": 20}, { "d": 30, "e": {"d": 10, "e": 11}}, ], "b2": { "c":[ {"d":10, "e": 12} ] } }, {"b": 2} ] } print list(Tree(a).execute("$.a[@.b is 1].c[@.d is 10]")) print list(Tree(a).execute("$.a[@.b is 1].c..*[@.d is 10]")) print list(Tree(a).execute("$.a[@.b is 1]..c[@.d is 10]")) Output: [] [{'e': 20, 'd': 10}, {'e': 11, 'd': 10}] [{'e': 20, 'd': 10}, {'e': 12, 'd': 10}]

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/adriank/ObjectPath/issues/65, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKycqU0xaJVGhiBfiRP9OXHNVOLWl0rks5uFSqpgaJpZM4VKN6v .