h2non / jsonpath-ng

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

Multiple "." periods in dictionary key name #67

Closed codedawi closed 3 years ago

codedawi commented 3 years ago

We are attempting to use this package to parse values from a json payload that has keys with periods (.) in the name. It does not seem to support these and I provided two example use cases below. These are both technically valid dict and json objects.

Input (Non-Nested):

{
    "data": {
        "entity": {
            "com.hello.DS": 123456 
        }
    }
}

Attempted JSONPath: data.entity['com.hello.DS'] and data.entity.['com.hello.DS']

Input (Nested):

{
    "data": {
        "entity": {
            "com.hello.DS": {
                "value": 123456
            }
        }
    }
}

Attempted JSONPath: data.entity['com.hello.DS'].value and data.entity.['com.hello.DS'].value

codedawi commented 3 years ago

Actually this works. We are just implementing this slightly differently that is probably causing the issue.

 from jsonpath_ng import jsonpath, parse

jsonpath_expr_1 = parse("data.entity['com.hello.DS']")
# ALSO WORKS "data.entity.['com.hello.DS'].value"
jsonpath_input_1 = {
    "data": {
        "entity": {
            "com.hello.DS": 123456
        }
    }
}

jsonpath_expr_2 = parse("data.entity['com.hello.DS'].value")
# ALSO WORKS "data.entity.['com.hello.DS'].value"

jsonpath_input_2 = {
    "data": {
        "entity": {
            "com.hello.DS": {
                "value": 123456
            }
        }
    }
}

# Extracting values is easy
result_1 = [match.value for match in jsonpath_expr_1.find(jsonpath_input_1)]
result_2 = [match.value for match in jsonpath_expr_2.find(jsonpath_input_2)]

import json
print(json.dumps(result_1)) # -> 123456
print(json.dumps(result_2)) # -> 123456