Multiple 'and' issues

54 views
Skip to first unread message

Karol Wybraniec

unread,
May 14, 2018, 7:20:49 AM5/14/18
to ObjectPath
Hi Adrian,

I hope, this project is still alive. I'm using objectpath with yamls. In most simple cases works well, but when I try to put 3 'and's in filtering it fails (these conditions separately returns the same value, all of them - fails). Just being curious if you've faced any issues with such a cases?

Regards,
Karol

Adrian Kalbarczyk

unread,
May 14, 2018, 7:24:11 AM5/14/18
to reaper.w...@gmail.com, Objectpath
Give me an example query, please. 

Greetings,
Adrian Kalbarczyk

http://kalbarczyk.co


--
You received this message because you are subscribed to the Google Groups "ObjectPath" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectpath+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Karol Wybraniec

unread,
May 14, 2018, 7:33:51 AM5/14/18
to ObjectPath
Please take a look at the code below. Using @.author.auth[0] is "Herman Melville" and @.author.auth[1] is "Herman Melville2" separately works. Using them together does not. Maybe it's one of yours assumptions. If so, let me know.

import json
import objectpath

my_json
= '''
{
    "store": {
        "book": [
            {
                "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
            },
            {
                "category": "fiction",
                "author":
                [
                                     {"auth": "Herman Melville"},
                                     {"auth": "Herman Melville2"}
                                ],
                "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(json.loads(my_json))
result
= tree.execute('$..book[@.price < 9 and @.author.auth[0] is "Herman Melville" and @.author.auth[1] is "Herman Melville2"]')
for each in result:
   
print(each)



Karol Wybraniec

unread,
May 14, 2018, 7:56:52 AM5/14/18
to ObjectPath
Maybe I'll put simple statement that is the summary of my case:

I have 3 conditions, which called separately evaluates to True, and when called together - evaluates to False.

Boolean logical value of "True and True and True" is True.

Sn3akyP3t3

unread,
Dec 17, 2018, 10:33:40 AM12/17/18
to ObjectPath
I'm not experienced with xpath so maybe I have a misunderstanding in my own query, but seeing this and comparing to my own they have the same structure with a similar expected result not found.

Is there something incorrect about how the "and" operator should be getting used?  I haven't seen a reply to this post yet so I'm assuming its still an issue or the OP found a solution and moved on.

Sn3akyP3t3

unread,
Dec 17, 2018, 10:39:31 AM12/17/18
to ObjectPath
Actually, I think @Karol's case may be a misunderstanding.  This seems to produce the expected result:
'$..book[@.price < 9 and @.author.auth[0] is "Herman Melville" or @.author.auth[1] is "Herman Melville2"]'

Replace the 2nd "and" with an "or".

Sn3akyP3t3

unread,
Dec 17, 2018, 11:08:40 AM12/17/18
to ObjectPath
My issue also appeared to be a misunderstanding of the language syntax.  What would be a good place to learn proper usage of the expected syntax for ObjectPath?  My issue was I was missing a dot to allow nested object inspection (@..contact vs @.contact).
Reply all
Reply to author
Forward
0 new messages