Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94773 > unrolled thread
| Started by | subhabrata.banerji@gmail.com |
|---|---|
| First post | 2015-07-30 06:32 -0700 |
| Last post | 2015-07-31 12:36 +0000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
Logical Query JSON subhabrata.banerji@gmail.com - 2015-07-30 06:32 -0700
Re: Logical Query JSON Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-30 15:48 +0000
Re: Logical Query JSON subhabrata.banerji@gmail.com - 2015-07-30 09:25 -0700
Re: Logical Query JSON dieter <dieter@handshake.de> - 2015-07-31 08:07 +0200
Re: Logical Query JSON Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-31 12:36 +0000
| From | subhabrata.banerji@gmail.com |
|---|---|
| Date | 2015-07-30 06:32 -0700 |
| Subject | Logical Query JSON |
| Message-ID | <cd8de52a-b0cf-45f8-8d07-9867ff4f6aa4@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-07-30 15:48 +0000 |
| Message-ID | <mpdh10$108$2@dont-email.me> |
| In reply to | #94773 |
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, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | subhabrata.banerji@gmail.com |
|---|---|
| Date | 2015-07-30 09:25 -0700 |
| Message-ID | <36c251da-3dc0-47ee-8831-c6ae5d092aa1@googlegroups.com> |
| In reply to | #94780 |
On Thursday, July 30, 2015 at 9:20:35 PM UTC+5:30, Denis McMahon wrote:
> 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
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.
[toc] | [prev] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2015-07-31 08:07 +0200 |
| Message-ID | <mailman.1104.1438322864.3674.python-list@python.org> |
| In reply to | #94782 |
subhabrata.banerji@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.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-07-31 12:36 +0000 |
| Message-ID | <mpfq54$3lv$1@dont-email.me> |
| In reply to | #94797 |
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, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web