Skip to main content

Python bindings for JSONata

Project description

Python bindings for JSONata.

Usage

from pyjsonata import jsonata

my_expression = "$"
my_json = "{'foo': 'bar'}"

# "{'foo': 'bar'}"
result = jsonata(my_expression, my_json)

With exception handling:

from pyjsonata import jsonata, PyjsonataError

my_expression = "$"
my_json = "{'foo': 'bar'}"

try:
    # "{'foo': 'bar'}"
    result = jsonata(my_expression, my_json)
except PyjsonataError as e:
    print("Error: ", e)

That’s it! Return values are always strings.

About

The reference implementation for JSONata is written in JavaScript. I have a separate library that makes this accessible from C via Duktape. This is a Python wrapper that calls into that library using Python’s built-in ctypes library, which should be portable to most Python interpreters.

At first I tried using py_mini_racer to run the JSONata library, but that package is around 40mb because it ships the complete V8 JavaScript runtime. In contrast this library is about 650k.

Building Packages

Source

Source packages are currently broken until I can be bothered to rewrite jsonata-c’s Makefile in Python, as required by setuptools.

🖕 setuptools.

Wheels

pyjsonata can be built to the manylinux2014 standard. There is no Windows support at this time.

The standard way to build manylinux2014 compatible extensions is with a bunch of Centos 7 Docker containers. The idea is that by linking against Centos 7 libc, the resultant binaries will be “portable enough” to modern systems. You don’t have to use these, but it’s not a bad idea.

Without Docker

  • Install gcc, patchelf and make from your distro repository

  • Install Python build deps:

    python3 -m pip install setuptools wheel auditwheel

To build:

git submodule update --init --recursive
python3 -m setup.py bdist_wheel
cd dist && auditwheel repair ./*.whl
mv wheelhouse/*.whl .

This will make you manylinux2014 wheels. These wheels are tagged to your specific Python version and ABI, like cp37-cp37m, but in reality, they should be py3-none. I can’t figure out how to make setuptools understand that. I think you can safely manually re-tag these by unzipping the wheel, editing the metadata in the WHEEL file, rezipping it, and changing the tag in the filename, but I haven’t yet tested whether that yields the desired results.

However, the arch tag, e.g. x86_64, aarch64, armv7l etc, is necessary.

With Docker

./build.sh <arch>

arch must be one of the architectures for which manylinux2014 build containers are provided. For example, if you are building on aarch64:

./build.sh aarch64

This will download the appropriate container and run the build. Built wheels are in the dist directory.

If you are me:

python3 -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*manylinux2014_*.whl

Testing

Pytest

From the repository root:

python3 -m pip install pytest
python3 -m pytest

Project details


Download files

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

Source Distributions

No source distribution files available for this release. See tutorial on generating distribution archives.

Built Distributions

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page