Questions tagged [jsonpath-ng]

The JSONPath Next-Generation (jsonpath-ng) library provides a robust and significantly extended implementation of JSONPath for Python that aims to be standard compliant, including arithmetic and binary comparison operators, as defined in the original JSONPath proposal.

This library differs from other JSONPath implementations in that it is a full language implementation, meaning the JSONPath expressions are first class objects, easy to analyze, transform, parse, print, and extend.

The jsonpath-ng packages merges both jsonpath-rw and jsonpath-rw-ext and provides several AST API enhancements, such as the ability to update or removes nodes in the tree.

Common Pitfalls

Extended functionality (like the use of arithmetic operators) requires including the extended parser instead of the standard parser:

#from jsonpath_ng import parse
from jsonpath_ng.ext import parse

Resources

27 questions
4
votes
1 answer

How to use jsonpath-ng arithmetic?

jsonpath-ng package claims to support basic arithmetic (https://pypi.org/project/jsonpath-ng/), but the parser won't accept arithmetic statements. Here is one of them: from jsonpath_ng import parse jsonpath_expr = parse('$.objects[*].cow +…
EzPizza
  • 979
  • 1
  • 13
  • 22
3
votes
2 answers

Update json data with context in Python using jsonpath-ng

following Update json nodes in Python using jsonpath, would like to know how one might update the JSON data given a certain context. So, say we pick the exact same JSON example: { "SchemeId": 10, "nominations": [ { …
Eyal
  • 48
  • 7
2
votes
0 answers

Using jsonpath-ng to extract keys and values with conditional matches

I have a data structure like this and want to extract nested data using conditional expressions. from jsonpath_ng.ext import parse ex = { "data": { "a": { "id": 1, "selected": False, "content": { …
goerlitz
  • 505
  • 4
  • 15
2
votes
0 answers

Compare two JSONpaths with and without wildcards

I'd like to programmatically compare two JSONPaths, preferably using the Python library jsonpath_ng as it is already used by the project. I need to be able to tell for example, whether the item pointed to by this…
James Bradbury
  • 1,708
  • 1
  • 19
  • 31
2
votes
2 answers

JSONPath - Filter expression is not working as expected

Can't get a JSONPath to work. JSON: { "data": [ { "meta": { "definition": { "title": "ID", "type": "text", "key": "657876498" } }, "attributes": { "id": "8606798", …
beginner_
  • 7,230
  • 18
  • 70
  • 127
2
votes
1 answer

How do we use regex filter in jsonpath_ng python, it treats / as SORT Direction. any alternative?

I'm trying to find the Worker whose Worker Value contains the number 880099 as substring. Here i'm trying to find priyanka's details using her employee ID. My Code . json_data = json.loads(json_string) from jsonpath_ng.ext import parser query =…
2
votes
1 answer

Adding a new node using jsonpath_ng

I would like to add a node to section of JSON after navigating to a particular section of it using jsonpath_ng as shown below: import json import jsonpath.ext as jspathExt import pprint jsStr = """ { "service_group": "default", "services": [ …
Amit Gupta
  • 163
  • 9
1
vote
1 answer

jsonpath: it possible to combine results into an object?

With jq I can use add to combine results. I am looking for the jsonpath equivalent of this: $ json="{\"nodes\": [{\"code\": \"DE\", \"label\": \"Germany\", \"ignore\": \"i1\"}, {\"code\": \"NL\", \"label\": \"Netherlands\", \"ignore\": \"i2\"}]}" $…
1
vote
1 answer

getting error "TypeError: must be str, not dict" when substituting variable into jsonpath_ng find string

Hi hope someone can help . I am fairly new to python and would like to pass a variable to a jsonpath_ng find function. In my code if I hardcode the json filter it works and I get a result and the expression type is "jsonpath_ng.jsonpath.Child" but…
Gursi-ng
  • 11
  • 1
1
vote
1 answer

Why do divisions not work in jsonpath-ng?

Jsonpath-ng offers basic arithmetic as in: from jsonpath_ng import jsonpath from jsonpath_ng.ext import parse jsonpath_expr = parse('$.foo * 2') target = {'foo': 2} result = jsonpath_expr.find(target) result = [match.value for match in…
EzPizza
  • 979
  • 1
  • 13
  • 22
1
vote
1 answer

Python jsonpath-ng : How to build a json document from jsonpath and values?

Developing in python, I would like to create a json document (tree) from a list of jsonpath and corresponding values. For example, from the jsonpath "$.orders[0].client" and a value "acme", it would create: {'orders':[{'client':'acme'}]} And…
1
vote
1 answer

Is it possible to do nested search in jsonpath-ng?

Source "mapping.json": { "result": { "src_color": "test_rule_2" }, "rules": { "color_degree": { "test_rule_1": { "color": 1 }, "test_rule_2": { "color": 2 } } } } So it works…
Ruslan
  • 43
  • 4
1
vote
2 answers

find parent for a json object using jsonpath-ng

I have a json obj like below. I need to find all the 'Statement's and respective parent. I was able to find the 'Statement's. But, do not know how to extract their respective parent. I am using Python3, jsonpath_ng. Here is my code so far: from…
user1717931
  • 2,419
  • 5
  • 29
  • 40
0
votes
1 answer

Python JSON parsing using jsonpath_ng - Filtering and Finding length

I have a JSON that I am trying to do some filtering and then count the number of elements returned. However it seems to work incorrectly. Here is the sample data and code to replicate. import json from jsonpath_ng.ext import * data_json = """ { …
0
votes
0 answers

JsonPath look for child element, but fallback to root element if child element is not found

How do I create json path syntax\query that tries and find a child element, but if it can't find it falls back to the root element? I've tried the…
Erwin
  • 1
  • 1
1
2