Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonpath-ng does not recognize same expressions as jsonpath.com and jsonpath-python #124

Open
dcsoft4 opened this issue Apr 17, 2023 · 0 comments

Comments

@dcsoft4
Copy link

dcsoft4 commented Apr 17, 2023

Depending on how the expressions are formatted, jsonpath-ng (1.5.3) does not always find the desired results the same as jsonpath.com and the jsonpath-python library.

import jsonpath_ng

data = {
  "keyframeMap": {
    "1": {
      "infoMap": {
        "main": {
          "colorCodeMap": {
            "1": {
              "hex": "#ffc284"
            },
            "2": {
              "hex": "#001384"
            }
          }
        },
        "alt": {
          "colorCodeMap": {
            "1": {
              "hex": "#00ff00"
            },
            "2": {
              "hex": "#808080"
            }
          }
        }
      }
    },
    "2": {
      "infoMap": {
        "main": {
          "colorCodeMap": {
            "1": {
              "hex": "#012345"
            },
            "2": {
              "hex": "#00ffff"
            }
          }
        },
        "alt": {
          "colorCodeMap": {
            "1": {
              "hex": "#000000"
            },
            "2": {
              "hex": "#ffffff"
            }
          }
        }
      }
    }
  }
}


def test_jsonpath_ng(expr, data) -> bool:
    try:
        res = [match.value for match in jsonpath_ng.parse(expr).find(data)]
    except:
        res = False

    return res and len(res)     # successful if a non-empty list was returned (some data was found)


def main():
    expr_notations = {
        # jsonpath-ng: bad (no match)
        # jsonpath.com ok
        'bracket': '$.keyframeMap[*].infoMap[main].colorCodeMap[1]',
        'dot': '$.keyframeMap.*.infoMap.main.colorCodeMap.1',

        # jsonpath-ng: ok
        # jsonpath.com ok
        'quoted_bracket': '$.keyframeMap["*"].infoMap["main"].colorCodeMap["1"]',

        # jsonpath_ng ok
        # jsonpath.com bad (no match)
        'quoted_dot': '$.keyframeMap."*".infoMap.main.colorCodeMap."1"',
        'quoted_dot2': '$.keyframeMap.*.infoMap.main.colorCodeMap."1"',
    }

    assert (not test_jsonpath_ng(expr_notations['bracket'], data))
    assert (not test_jsonpath_ng(expr_notations['dot'], data))
    assert (test_jsonpath_ng(expr_notations['quoted_bracket'], data))
    assert (test_jsonpath_ng(expr_notations['quoted_dot'], data))
    assert (test_jsonpath_ng(expr_notations['quoted_dot2'], data))


if __name__ == "__main__":
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant