Skip to main content

A lightweight, flexible, and expandable JSON query language

Project description

jsonquery-python

JSON Query Logo

This is a Python implementation of JSON Query, a small, flexible, and expandable JSON query language.

Try it out on the online playground: https://jsonquerylang.org

JSON Query Overview

Install

Install via PyPi: https://pypi.org/project/jsonquerylang/

pip install jsonquerylang

Use

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

Syntax

The JSON Query syntax is described on the following page: https://github.com/jsonquerylang/jsonquery?tab=readme-ov-file#syntax.

API

jsonquery

Compile and evaluate a JSON query.

Syntax:

jsonquery(data, query [, options])

Where:

  • data is a JSON object or array
  • query is a JSON query or string containing a text query
  • options is an optional object which can have the following options:
    • functions an object with custom functions

Example:

from pprint import pprint
from jsonquerylang import jsonquery

input = [
    {"name": "Chris", "age": 23, "scores": [7.2, 5, 8.0]},
    {"name": "Joe", "age": 32, "scores": [6.1, 8.1]},
    {"name": "Emily", "age": 19},
]
query = ["sort", ["get", "age"], "desc"]
output = jsonquery(input, query)
pprint(output)
# [{'age': 32, 'name': 'Joe', 'scores': [6.1, 8.1]},
#  {'age': 23, 'name': 'Chris', 'scores': [7.2, 5, 8.0]},
#  {'age': 19, 'name': 'Emily'}]

compile

Compile a JSON Query. Returns a function which can execute the query repeatedly for different inputs.

Syntax:

compile(query [, options])

Where:

  • query is a JSON query or string containing a text query
  • options is an optional object which can have the following options:
    • functions an object with custom functions

The function returns a lambda function which can be executed by passing JSON data as first argument.

Example:

from pprint import pprint
from jsonquerylang import compile

input = [
    {"name": "Chris", "age": 23, "scores": [7.2, 5, 8.0]},
    {"name": "Joe", "age": 32, "scores": [6.1, 8.1]},
    {"name": "Emily", "age": 19},
]
query = ["sort", ["get", "age"], "desc"]
queryMe = compile(query)
output = queryMe(input)
pprint(output)
# [{'age': 32, 'name': 'Joe', 'scores': [6.1, 8.1]},
#  {'age': 23, 'name': 'Chris', 'scores': [7.2, 5, 8.0]},
#  {'age': 19, 'name': 'Emily'}]

parse

Parse a string containing a JSON Query into JSON.

Syntax:

parse(textQuery, [, options]) 

Where:

  • textQuery: A query in text format
  • options: An optional object which can have the following properties:
    • functions an object with custom functions
    • operators an object with the names of custom operators both as key and value

Example:

from pprint import pprint
from jsonquerylang import parse

text_query = '.friends | filter(.city == "new York") | sort(.age) | pick(.name, .age)'
json_query = parse(text_query)
pprint(json_query)
# ['pipe',
#  ['get', 'friends'],
#  ['filter', ['eq', ['get', 'city'], 'New York']],
#  ['sort', ['get', 'age']],
#  ['pick', ['get', 'name'], ['get', 'age']]]

stringify

Stringify a JSON Query into a readable, human friendly text format.

Syntax:

stringify(query [, options])

Where:

  • query is a JSON Query
  • options is an optional object that can have the following properties:
    • operators an object with the names of custom operators both as key and value
    • indentation a string containing the desired indentation, defaults to two spaces: " "
    • max_line_length a number with the maximum line length, used for wrapping contents. Default value: 40.

Example:

from jsonquerylang import stringify

jsonQuery = [
    "pipe",
    ["get", "friends"],
    ["filter", ["eq", ["get", "city"], "New York"]],
    ["sort", ["get", "age"]],
    ["pick", ["get", "name"], ["get", "age"]],
]
textQuery = stringify(jsonQuery)
print(textQuery)
# '.friends | filter(.city == "new York") | sort(.age) | pick(.name, .age)'

License

Released under the ISC license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonquerylang-1.1.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonquerylang-1.1.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file jsonquerylang-1.1.1.tar.gz.

File metadata

  • Download URL: jsonquerylang-1.1.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for jsonquerylang-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b66cc9cae12135fe9fb7f6e2d4f1f703685806223a180d85d695afa414b9d2ba
MD5 5582bfc61b982ed3d0f48176cc94f124
BLAKE2b-256 a1492bc349923d322ef682391c149b59f1e766356b3c98e340233a3cc8c5edc9

See more details on using hashes here.

File details

Details for the file jsonquerylang-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: jsonquerylang-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for jsonquerylang-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40906259692439fce4da345f9175e455a820b38c87d738b8bd9567078644f3d8
MD5 e6573294e57c261dec3c1cb45d276ae0
BLAKE2b-256 378ad4ef935f41b13b617fb72a27a2ca7f0d950cb777fabe4e57b121ed688195

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page