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

how to remove a node with jsonpath? #42

Closed LQBing closed 1 year ago

LQBing commented 4 years ago

I need remove a node with a not special jsonpath, but I do not find any function looks like remove or delete. who can help me?

LQBing commented 4 years ago

I need make the json {'a': 'a', 'b': 'b'} remove a node 'b', the json need be {'a': 'a'}.

If use update() like blow code, the json will be {'a': 'a', 'b': None}. the node 'b' will still there.

json1 = {'a': 'a', 'b': 'b'} expr = parse('b') res = expr.update(json1, [])

I know the dict().pop(), but the jsonpath is not static. So dict().pop() is not working.

baynes commented 4 years ago

Hopefully the issue #44 which I have just raised will answer this. This will be a good test of the text I have proposed.

WilliamYuhangLee commented 4 years ago

I was wondering this as well, until I saw filter() in another issue #44. However it turns out filter() is bugged: The filter() function does not work when there is a filter expression in the jsonpath, and it throws a NotImplementedError. Code to duplicate this error:

obj = {
    "a": [
        {"b": "X"},
        {"b": "Y"}
    ]
}

jsonpath = parse("$.a[?(@.b==X)]")

matches = jsonpath.find(obj)
print(matches[0].value)
>>> {'b': 'X'}  # find() returns correct value

result = jsonpath.filter(lambda x: True, obj)
>>> NotImplementedError  # filter() throws error
cmin764 commented 2 years ago

Any news on this? Meanwhile I'm trying to get a temporary solution into this library.


L.E.: Made a PR for it: https://github.com/h2non/jsonpath-ng/pull/106