adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Issue with attribute names consisting of a string with just a number inside? #95

Closed JanKoppe closed 2 years ago

JanKoppe commented 4 years ago

Hello, just encountered this while trying to write ObjectPath definitions for https://github.com/project-sunbird/prometheus-jsonpath-exporter.

The source JSON is generated by some proprietary software, and I am not able to change this (without additional layers/effort, which I would like to avoid). The issue here seems to be that the entire document is put inside a top-level element whose name consists of a string that contains just a number.

This number changes over time, so I wanted to use a wildcard operator, but this seems to behave differently from what I expect/understood from the docs - as far as I understand it, something like $.*.a should work just fine on {"123": {"a": 123}}, returning the value of $.123.a. Instead I'm just getting a None response.

But, also, if I explicitly try to address $.123.a it fails:

>>> js = json.loads('{"123": {"a": 123}}')
>>> tr = Tree(js)
>>> tr.execute("$.123.a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/objectpath/core/interpreter.py", line 731, in execute
    tree = self.compile(expr)
  File "/usr/lib/python3.8/site-packages/objectpath/core/interpreter.py", line 66, in compile
    ret = EXPR_CACHE[expr] = parse(expr, self.D)
  File "/usr/lib/python3.8/site-packages/objectpath/core/parser.py", line 489, in parse
    r = expression().getTree()
  File "/usr/lib/python3.8/site-packages/objectpath/core/parser.py", line 100, in getTree
    ret_append(i.getTree())
  File "/usr/lib/python3.8/site-packages/objectpath/core/parser.py", line 95, in getTree
    if type(self.fst.value) in NUM_TYPES and self.snd is None:
AttributeError: 'float' object has no attribute 'value'

This seems to be either very confusing or buggy behaviour to me. Any ideas what's going on? What would be the cleanest way in this case to reach a?

adriank commented 4 years ago

$."123".a

$.*[0].a is probably what you are looking for.

On Wed, 15 Jan 2020 at 09:20 Jan Koppe notifications@github.com wrote:

Hello, just encountered this while trying to write ObjectPath definitions for https://github.com/project-sunbird/prometheus-jsonpath-exporter.

The source JSON is generated by some proprietary software, and I am not able to change this (without additional layers/effort, which I would like to avoid). The issue here seems to be that the entire document is put inside a top-level element whose name consists of a string that contains just a number.

This number changes over time, so I wanted to use a wildcard operator, but this seems to behave differently from what I expect/understood from the docs - as far as I understand it, something like $.*.a should work just fine on {"123": {"a": 123}}, returning the value of $.123.a. Instead I'm just getting a None response.

But, also, if I explicitly try to address $.123.a it fails:

js = json.loads('{"123": {"a": 123}}') tr = Tree(js) tr.execute("$.123.a") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.8/site-packages/objectpath/core/interpreter.py", line 731, in execute tree = self.compile(expr) File "/usr/lib/python3.8/site-packages/objectpath/core/interpreter.py", line 66, in compile ret = EXPR_CACHE[expr] = parse(expr, self.D) File "/usr/lib/python3.8/site-packages/objectpath/core/parser.py", line 489, in parse r = expression().getTree() File "/usr/lib/python3.8/site-packages/objectpath/core/parser.py", line 100, in getTree ret_append(i.getTree()) File "/usr/lib/python3.8/site-packages/objectpath/core/parser.py", line 95, in getTree if type(self.fst.value) in NUM_TYPES and self.snd is None: AttributeError: 'float' object has no attribute 'value'

This seems to be either very confusing or buggy behaviour to me. Any ideas what's going on? What would be the cleanest way in this case to reach a?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/adriank/ObjectPath/issues/95?email_source=notifications&email_token=AABLE4UTKTVGALUYORAIGADQ53BNFA5CNFSM4KG7PHCKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IGI27DA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLE4TF6XJSXD5X6AWPUU3Q53BNFANCNFSM4KG7PHCA .

-- Greetings, Adrian Kalbarczyk

https://kalbarczyk.co | https://devyard.io