adriank / ObjectPath

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

Nested array query issue #94

Closed lucasvdev closed 2 years ago

lucasvdev commented 5 years ago

Hello,

I got a problem when query a key into a nested array.

data = [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            { 
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99,
                "super": {
                    "test": "lookingforthat"
                }
            },
            { 
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            { 
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ]

tree = objectpath.Tree(data)
result_ok = tree.execute("$.*[@.super.test is 'lookingforthat']")
result_nonok = tree.execute("$.*[@..test is 'lookingforthat']")

for entry in result_ok:
     print(entry)

# { 
#   "category": "fiction",
#   "author": "Evelyn Waugh",
#   "title": "Sword of Honour",
#   "price": 12.99,
#   "super": {
#       "test": "lookingforthat"
#   }
# } 

for entry in result_nonok:
     print(entry)

# Return nothing
adriank commented 5 years ago

This is probably a bug. .. operator returns a generator so I think 'lookingforthat' in @..test would work. Also, this particular bug could be already solved. Please check if Github version of ObjectPath works better for you. Greetings, Adrian Kalbarczyk

https://kalbarczyk.co | https://devyard.io

On Thu, Oct 31, 2019 at 11:41 AM Lucas VAGNIER notifications@github.com wrote:

Hello,

I got a problem when query a key into a nested array.

data = [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99, "super": { "test": "lookingforthat" } }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ]

tree = objectpath.Tree(data) result_ok = tree.execute("$.[@.super.test is 'lookingforthat']") result_nonok = tree.execute("$.[@..test is 'lookingforthat']") for entry in result_ok: print(entry)

{ # "category": "fiction",# "author": "Evelyn Waugh",# "title": "Sword of Honour",# "price": 12.99,# "super": {# "test": "lookingforthat"# }# }

for entry in result_nonok: print(entry)

Return nothing

— 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/94?email_source=notifications&email_token=AABLE4TRLYFLY4AXIN7EHWDQRKY3ZA5CNFSM4JHH4UN2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HVXJJFA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLE4SCVEDI2FPXBTJQ5UDQRKY3ZANCNFSM4JHH4UNQ .

lucasvdev commented 5 years ago

Thank your for your fast reply !

In fact, It's probably a bug and it's resolved by using your syntax :

result = tree.execute("$.*['lookingforthat' in @..test]")