Logical Query JSON

subhabrata.banerji at gmail.com subhabrata.banerji at gmail.com
Thu Jul 30 18:25:42 CEST 2015


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. 







More information about the Python-list mailing list