Logical Query JSON
dieter
dieter at handshake.de
Fri Jul 31 02:07:23 EDT 2015
subhabrata.banerji at 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.
More information about the Python-list
mailing list