Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Logical Query JSON

51 views
Skip to first unread message

subhabrat...@gmail.com

unread,
Jul 30, 2015, 9:32:18 AM7/30/15
to
Dear Group,

I am trying to query JSON with Logical operators.

I tried to experiment lot with it, but could not do much.
I came many times pretty close but missed it almost.

I tried to experiment with json, jsonquery, jsonschema, jsonpipe, objectpath, requests.

I got a good example from http://www-01.ibm.com/support/knowledgecenter/SSEPEK_11.0.0/com.ibm.db2z11.doc.json/src/tpc/db2z_jsonlogicaloperators.dita

But I was looking for one or two worked out examples to start with.

I am using Python2.7+ on Windows 7 with IDLE as GUI.

I am trying and if anybody of the esteemed members may kindly help me with.



Regards,
Subhabrata Banerjee.

Denis McMahon

unread,
Jul 30, 2015, 11:50:35 AM7/30/15
to
On Thu, 30 Jul 2015 06:32:01 -0700, subhabrata.banerji wrote:

> I am trying to query JSON with Logical operators.

Your post was an excellent example of asking for help without explaining
what your problem was at all.

Please:

- show an example of what you tried;

- give the results you expected;

- show the results you actually got.

COPY and PASTE the code and results, do not re-type them, or summarise
them.

I found the examples quite easy to follow to create json queries,
although as I don't have a db2 etc setup here iI'm unale to try feeding
the resulting json query into a database to see what comes out.

--
Denis McMahon, denismf...@gmail.com

subhabrat...@gmail.com

unread,
Jul 30, 2015, 12:26:06 PM7/30/15
to
Dear Sir,

I am trying to quote some of my exercises below, and my objective.

(1) Exercise with objectpath:
>>> from objectpath import *
>>> tree=Tree({"a":1})
>>> tree.execute("$.a")
1
>>> $
{
"a":1,
"b":{
"c":[1,2,3]
}
}
SyntaxError: invalid syntax
>>> x1={"a":1,"b":{"c":[1,2,3]}}
>>> x1.b

Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
x1.b
AttributeError: 'dict' object has no attribute 'b'
>>> x1."b"
SyntaxError: invalid syntax

(2) Exercise from IBM Example:

>>> x1={"or":[{"age":4},{"name":"Joe"}]}
>>> x2=x1
>>> print x2
{'or': [{'age': 4}, {'name': 'Joe'}]}
>>> x1['age']

Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
x1['age']
KeyError: 'age'
>>> x1['or']
[{'age': 4}, {'name': 'Joe'}]
>>> x1['or']['age']

Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
x1['or']['age']
TypeError: list indices must be integers, not str
>>> x1['or'][0]
{'age': 4}
>>> x1['or'][1]
{'name': 'Joe'}


My expectation is:

If I do AND, NOT, OR with two or more JSON values like
{"age":4}, {"name":"Joe"}, ...etc. then I should get recirprocal
results.
Considering each one as Python variable and applying logical Python
operation helps, but I am looking a smarter solution.

Apology for indentation error.

Regards,
Subhabrata Banerjee.





dieter

unread,
Jul 31, 2015, 2:08:51 AM7/31/15
to pytho...@python.org
subhabrat...@gmail.com writes:
> ...
> I am trying to quote some of my exercises below, and my objective.

A general remark. Python errror messages are quite good (unlike e.g.
many Microsoft or Oracle error messages). You can almost always trust
them.

Thus, if you get a "SyntaxError", something with your Python syntax
is wrong (Python is *very* sensitive to leading spaces, take case
to get the correct indentation).

An "AttributeError" means that you used the attribute access syntax
("obj.attr") for something which was not an attribute.

Etc...

> (1) Exercise with objectpath:
>>>> from objectpath import *
>>>> tree=Tree({"a":1})
>>>> tree.execute("$.a")
> 1
>>>> $
> {
> "a":1,
> "b":{
> "c":[1,2,3]
> }
> }
> SyntaxError: invalid syntax

This looks a bit strange: "$" is not a Python name - thus,
it could give a "SyntaxError" - but then, you should not see the
dict like representation between the "$" and the "SyntaxError".
Anyway, the dict like representation looks out of place - where does
it come from?

>>>> x1={"a":1,"b":{"c":[1,2,3]}}
>>>> x1.b
>
> Traceback (most recent call last):
> File "<pyshell#46>", line 1, in <module>
> x1.b
> AttributeError: 'dict' object has no attribute 'b'

"x1" is a dict and access to its keys is via subscription syntax
("mapping[key]"), not the attribute access syntax ("obj.attr").

>>>> x1."b"
> SyntaxError: invalid syntax

The "attr" in the attribute access syntax "obj.attr" must be
a Python name, not an expression (such as the string '"b"').

The correct access to "b" would be 'x["b"]'.


> (2) Exercise from IBM Example:
>
>>>> x1={"or":[{"age":4},{"name":"Joe"}]}
>>>> x2=x1
>>>> print x2
> {'or': [{'age': 4}, {'name': 'Joe'}]}
>>>> x1['age']
>
> Traceback (most recent call last):
> File "<pyshell#27>", line 1, in <module>
> x1['age']
> KeyError: 'age'

"x1" is a dict with a single key "or". The value for "or"
is a list with two dicts. The get the dict for "age",
you would use 'x1["or"][0]'; to get the age value
'x1["or"][0]["age"]'.

>>>> x1['or']
> [{'age': 4}, {'name': 'Joe'}]
>>>> x1['or']['age']
>
> Traceback (most recent call last):
> File "<pyshell#29>", line 1, in <module>
> x1['or']['age']
> TypeError: list indices must be integers, not str

'x1["or"]' is a list, not a dict.
>>>> x1['or'][0]
> {'age': 4}
>>>> x1['or'][1]
> {'name': 'Joe'}

You are making progress....

>
> My expectation is:
>
> If I do AND, NOT, OR with two or more JSON values like
> {"age":4}, {"name":"Joe"}, ...etc. then I should get recirprocal
> results.
> Considering each one as Python variable and applying logical Python
> operation helps, but I am looking a smarter solution.

You might need to read through the Python tutorial - in order
to get a thorough understanding of

* attribute versus subscription access,
* Python's simple data types (such as "dict"s, "list"s, etc.)
and how they are represented when printed
* what the various error messages (such as "SyntaxError",
"AttributeError", "KeyError", "TypeError", ...) mean.

Keep in mind that Python is a (more or less) "general purpose" language
which does not know about "jsonquery". It has "and", "or" and "not"
operators (defined in the language reference) *BUT* these are not
the operators you are looking for.
You will need a special "jsonquery" extension (search
"http://pypi.python.org" to find out whether there is something like this)
which would provide appropriate support.

Denis McMahon

unread,
Jul 31, 2015, 8:38:44 AM7/31/15
to
On Fri, 31 Jul 2015 08:07:23 +0200, dieter wrote:

> Keep in mind that Python is a (more or less) "general purpose" language
> which does not know about "jsonquery". It has "and", "or" and "not"
> operators (defined in the language reference) *BUT* these are not the
> operators you are looking for.
> You will need a special "jsonquery" extension (search
> "http://pypi.python.org" to find out whether there is something like
> this)
> which would provide appropriate support.

Actually it's not too hard. You can construct the json query syntax
fairly easily from python once you understand it:

>>> import json

>>> query = json.dumps( { "$and":[ { "$gt": {"age": 5} }, { "$not":
{"name": "curly"} } ] } )

>>> query

'{"$and": [{"$gt": {"age": 5}}, {"$not": {"name": "curly"}}]}'

--
Denis McMahon, denismf...@gmail.com
0 new messages