h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
564 stars 85 forks source link

JsonPathParserError, when Objecttag contains only Number #166

Closed sm-ffm closed 3 months ago

sm-ffm commented 3 months ago

I've got the following Error - maybe a bug?:

jsonpath_ng.exceptions.JsonPathParserError: Parse error at 1:2 near token 1 (NUMBER)

I tried a lot, and it only raises, when Objectag only contains Numbers. I tried it with parsing / finding Data and Editing Data. Both raises Error.

Example Code:

import json
from jsonpath_ng import jsonpath, parse

def jsonparser(Path, Json):
    json_data = json.loads(Json)
    jsonpath_expression = parse(Path)
    Output = jsonpath_expression.find(json_data)[0].value
    return Output

def jsonEditvalue(Path, Json, Value):
    json_data = json.loads(Json)
    jsonpath_expr = parse(Path)
    jsonpath_expr.find(json_data)
    jsonpath_expr.update(json_data, Value)
    Output = json.dumps(json_data, indent=2)
    return Output

jsonstring = """
    {
    "1" : {
        "One" : "Data1",
        "Two" : "Data2",
        "Three" : "Data3"
    },
    "2" : {
        "One" : "Data1",
        "Two" : "Data2",
        "Three" : "Data3"
    }
}
"""
print(jsonparser("$.1.One", jsonstring))
print(jsonEditvalue("$.1.One", jsonstring, "DataNew"))

jsonpath-ng is the first Parser I used with that Kind of Issue.

michaelmior commented 3 months ago

If you want to use keys that are only numbers, you should quote them

print(jsonparser("$.\"1\".One", jsonstring))
print(jsonEditvalue("$.\"1\".One", jsonstring, "DataNew"))