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

bc-jsonpath-ng does not recognize same expressions as jsonpath.com (and jsonpath-python) #24

Closed
dcsoft4 opened this issue Apr 17, 2023 · 2 comments

Comments

@dcsoft4
Copy link

dcsoft4 commented Apr 17, 2023

Thanks for your fork, I saw your comment on h2non's github so I take it you are active. I am duplicating my issue as reported there, as it also happens with your fork. Thanks much!

import bc_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 bc_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()
@dcsoft4 dcsoft4 changed the title bc-jsonpath-ng does not recognize same expressions as jsonpath.com and jsonpath-python bc-jsonpath-ng does not recognize same expressions as jsonpath.com (and jsonpath-python) Apr 17, 2023
@gruebel
Copy link
Contributor

gruebel commented Apr 26, 2023

Hey @dcsoft4 thanks for reaching out.

I tested your code and it passes for me.

I can also recommend to try out the extended jsonpath parser, which has some extra features compared to the normal one

from bc_jsonpath_ng.ext import parser

parser.parse(expr).find(data)

@gruebel
Copy link
Contributor

gruebel commented May 9, 2023

feel free to reach out again.

@gruebel gruebel closed this as completed May 9, 2023
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

2 participants