JSON Query Language for Python

2.1.0 · active · verified Thu Apr 16

jsonquerylang is a lightweight, flexible, and expandable JSON query language implemented in Python. It allows users to query JSON data using a human-friendly text format or an intermediate JSON format. The library is currently at version 2.1.0 and is actively maintained with a focus on feature richness and interoperability.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `jsonquery` with both text-based and JSON-formatted queries. It also shows basic usage of `parse` and `stringify` to convert between query formats.

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"}
    ]
}

# Query using text format
output_text = jsonquery(data, """
.friends | filter(.city == "New York") | sort(.age) | pick(.name, .age)
""")
print("Text Query Result:")
pprint(output_text)

# Query using JSON format
output_json = jsonquery(data, [
    "pipe",
    ["get", "friends"],
    ["filter", ["eq", ["get", "city"], "New York"]],
    ["sort", ["get", "age"]],
    ["pick", "get", "name"], ["get", "age"]
])
print("\nJSON Query Result:")
pprint(output_json)

# Example of parsing and stringifying
from jsonquerylang import parse, stringify
text_query = '.friends | filter(.age > 20)'
parsed_query = parse(text_query)
print(f"\nParsed query: {parsed_query}")
round_tripped_text = stringify(parsed_query)
print(f"Round-tripped text: {round_tripped_text}")

view raw JSON →