Skip to main content

JSONPath selectors for Python Liquid.

Project description

Liquid JSONPath

JSONPath selectors for Python Liquid.

License
PyPi - Version Python versions
Tests Coverage


Table of Contents

Installation

Install JSONPath for Liquid using pip:

python -m pip install -U liquid-jsonpath

Or pipenv:

pipenv install liquid-jsonpath

Links

Examples

Filter

This example adds the find filter to a Liquid environment. You can think of find as an advanced alternative to the standard map and where filters. It takes a JSONPath string argument and applies it to the filter's left value.

from liquid import Environment
from liquid_jsonpath import Find

env = Environment()
env.add_filter("find", Find())

data = {
    "users": [
        {
            "name": "Sue",
            "score": 100,
        },
        {
            "name": "John",
            "score": 86,
        },
        {
            "name": "Sally",
            "score": 84,
        },
        {
            "name": "Jane",
            "score": 55,
        },
    ]
}

template = env.from_string("{{ data | find: '$.users.*.name' | join: ' ' }}")
print(template.render(data=data))  # Sue John Sally Jane

Tag

This example replaces the standard {% for %} tag with one that supports piping an iterable through a JSONPath expression.

from liquid import Environment
from liquid_jsonpath import JSONPathForTag

env = Environment()
env.add_tag(JSONPathForTag)

data = {
    "users": [
        {
            "name": "Sue",
            "score": 100,
        },
        {
            "name": "John",
            "score": 86,
        },
        {
            "name": "Sally",
            "score": 84,
        },
        {
            "name": "Jane",
            "score": 55,
        },
    ]
}

template = env.from_string(
    "{% for name in data | '$.users.*.name' %}"
    "{{ name }}, "
    "{% endfor %}"
)
print(template.render(data=data))  # Sue, John, Sally, Jane,

License

liquid-jsonpath is distributed under the terms of the MIT 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

liquid_jsonpath-0.2.0.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

liquid_jsonpath-0.2.0-py3-none-any.whl (8.4 kB view hashes)

Uploaded Python 3

Supported by

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