from jsonquerylang import jsonquery
from pprint import pprint
data = {
"friends": [
{"name": "Chris", "age": 23, "city": "New York"},
{"name": "Emily", "age": 19, "city": "Atlanta"},
{"name": "Joe", "age": 32, "city": "New York"},
{"name": "Kevin", "age": 19, "city": "Atlanta"},
{"name": "Michelle", "age": 27, "city": "Los Angeles"},
{"name": "Robert", "age": 45, "city": "Manhattan"},
{"name": "Sarah", "age": 31, "city": "New York"}
]
}
# Get the array containing the friends from the object, filter the friends that live in New York,
# sort them by age, and pick just the name and age out of the objects.
output = jsonquery(data, """
.friends
| filter(.city == "New York")
| sort(.age)
| pick(.name, .age)
""")
pprint(output)
# [{'age': 23, 'name': 'Chris'},
# {'age': 31, 'name': 'Sarah'},
# {'age': 32, 'name': 'Joe'}]
# The same query can be written using the JSON format instead of the text format.
# Note that the functions `parse` and `stringify` can be used
# to convert from text format to JSON format and vice versa.
pprint(jsonquery(data, [
"pipe",
["get", "friends"],
["filter", ["eq", ["get", "city"], "New York"]],
["sort", ["get", "age"]],
["pick", ["get", "name"], ["get", "age"]]
]))
# [{'age': 23, 'name': 'Chris'},
# {'age': 31, 'name': 'Sarah'},
# {'age': 32, 'name': 'Joe'}]
The functions jsonquery, compile, and parse accept custom functions. Custom functions are passed as an object with the key being the function name, and the value being a factory function.
Here is a minimal example which adds a function times to JSON Query:
In the example above, the argument value is static. When the parameters are not static, the function compile can be used to compile them. For example, the function filter is implemented as follows:
from jsonquerylang import compile, JsonQueryOptions
def truthy(value):
return value not in [False, 0, None]
def fn_filter(predicate):
_predicate = compile(predicate)
return lambda data: list(filter(lambda item: truthy(_predicate(item)), data))
options: JsonQueryOptions = {"functions": {"filter": fn_filter}}
The functions jsonquery, parse, and stringify accept custom operators. Custom operators are passed as a list with operators definitions. In practice, often both a custom operator and a corresponding custom function are configured. Each custom operator is an object with:
Two required properties name and op to specify the function name and operator name, for example { "name": "add", "op": "+", ... }
One of the three properties at, before, or after, specifying the precedence compared to an existing operator.
optionally, the property left_associative can be set to True to allow using a chain of multiple operators without parenthesis, like a and b and c. Without this, an exception will be thrown, which can be solved by using parenthesis like (a and b) and c.
optionally, the property vararg can be set to True when the function supports a variable number of arguments, like and(a, b, c, ...). In that case, a chain of operators like a and b and c will be parsed into the JSON Format ["and", a, b, c, ...]. Operators that do not support variable arguments, like 1 + 2 + 3, will be parsed into a nested JSON Format like ["add", ["add", 1, 2], 3].
Here is a minimal example configuring a custom operator ~= and a corresponding function aboutEq:
A final implementation of JSONPath for Python that aims to be standard compliant, including arithmetic and binary comparison operators and providing clear AST for metaprogramming.