Skip to main content

Pure Python implementation of JSONata

Project description

jsonata-python

Build Status PyPI

Pure Python implementation of JSONata.

This is a Python port of the JSONata reference implementation, and also borrows from the Dashjoin Java port.

This implementation supports 100% of the language features of JSONata, with no external dependencies. The JSONata documentation can be found here.

Installation

pipx install jsonata-python

Getting Started

A very simple start:

>>> import jsonata
>>> data = {"example": [{"value": 4}, {"value": 7}, {"value": 13}]}
>>> expr = jsonata.Jsonata("$sum(example.value)")
>>> result = expr.evaluate(data)
>>> result
24

Command Line Interface

The CLI provides the same functionality as the Dashjoin JSONata CLI.

% jsonata -h
usage: jsonata [-h] [-v] [-e <file>] [-i <arg>] [-ic <arg>] [-f {auto,json,string}] [-o <arg>] [-oc <arg>] [-time] [-c] [-b <json-string>]
                   [-bf <file>] [-it]
                   [expr]

Pure Python JSONata CLI

positional arguments:
  expr

options:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -e <file>, --expression <file>
                        JSON expression to evaluate.
  -i <arg>, --input <arg>
                        JSON input file (- for stdin)
  -ic <arg>, --icharset <arg>
                        Input character set (default=utf-8)
  -f {auto,json,string}, --format {auto,json,string}
                        Input format (default=auto)
  -o <arg>, --output <arg>
                        JSON output file (default=stdout)
  -oc <arg>, --ocharset <arg>
                        Output character set (default=utf-8)
  -time                 Print performance timers to stderr
  -c, --compact         Compact JSON output (don't prettify)
  -b <json-string>, --bindings <json-string>
                        JSONata variable bindings
  -bf <file>, --bindings-file <file>
                        JSONata variable bindings file
  -it, --interactive    Interactive REPL (requires input file)

Examples

% echo '{"a":"hello", "b":" world"}' | jsonata '(a & b)'
hello world

% echo '{"a":"hello", "b":" world"}' | jsonata -o helloworld.json $
# helloworld.json written

% ls | jsonata $
helloworld.json

% ps -o pid="",%cpu="",%mem="" | jsonata '$.$split(/\n/).$trim().[ $split(/\s+/)[$length()>0].$number() ]' -c
[[4105,0,0],[4646,0,0],[4666,0,0],[33696,0,0]...]

% curl -s https://raw.githubusercontent.com/jsonata-js/jsonata/master/test/test-suite/datasets/dataset1.json | jsonata '{"Name": FirstName & " " & Surname, "Cities": **.City, "Emails": Email[type="home"].address}'
{
  "Name": "Fred Smith",
  "Cities": [
    "Winchester",
    "London"
  ],
  "Emails": [
    "freddy@my-social.com",
    "frederic.smith@very-serious.com"
  ]
}

% jsonata -i helloworld.json -it
Enter an expression to have it evaluated.
JSONata> (a & b)
hello world

Running Tests

This project uses the repository of the reference implementation as a submodule. This allows referencing the current version of the unit tests. To clone this repository, run:

git clone --recurse-submodules https://github.com/rayokota/jsonata-python

To build and run the unit tests:

python3 -m pip install nox
nox --sessions tests

Notes

JSONata date/time functions that use ISO 8601 formats are only supported with Python 3.11+.

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

jsonata_python-0.6.3.tar.gz (364.4 kB view details)

Uploaded Source

Built Distribution

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

jsonata_python-0.6.3-py3-none-any.whl (84.1 kB view details)

Uploaded Python 3

File details

Details for the file jsonata_python-0.6.3.tar.gz.

File metadata

  • Download URL: jsonata_python-0.6.3.tar.gz
  • Upload date:
  • Size: 364.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for jsonata_python-0.6.3.tar.gz
Algorithm Hash digest
SHA256 fb62ce909a25c6a73d012f19d031348f8b63b2240d751062d8e1ef2e095637a7
MD5 97ce91bc7f13e9674ed1dfcc662beaa4
BLAKE2b-256 ba1b5a889abcd92dedecba3f0a5657c77d473869e046d217d597f1bbaf21e433

See more details on using hashes here.

File details

Details for the file jsonata_python-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: jsonata_python-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 84.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for jsonata_python-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e20426b17cde7740c75ff1c8895d5ca2edbdf5b022ad51e33b34ab693a33a28e
MD5 9fdf78cf21672a4401ff6b1b68e6dc36
BLAKE2b-256 1f80a83af0ee65fdda89c225d0cf832cc4aaa59f779cbe86a9d56937100fbbd5

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