Git Product home page Git Product logo

jsonpath-rust-bindings's Introduction

jsonpath-rust-bindings

PyPI - Downloads GitHub Workflow Status (with event) GitHub Workflow Status (with event) piwheels (including prereleases)

This package contains Python bindings for jsonpath-rust library by besok.

The details regarding the JsonPath itself can be found here.

Installation

pip install jsonpath-rust-bindings

Usage

from jsonpath_rust_bindings import Finder

sample = {
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95,
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99,
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99,
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99,
            },
        ],
        "bicycle": {"color": "red", "price": 19.95},
    },
    "expensive": 10,
}

queries = [
    '$.store.book[*].author',
    '$..book[?(@.isbn)]',
    '$.store.*',
    '$..author',
    '$.store..price',
    '$..book[2]',
    # '$..book[-2]',
    '$..book[0,1]',
    '$..book[:2]',
    '$..book[1:2]',
    '$..book[-2:]',
    '$..book[2:]',
    '$.store.book[?(@.price<10)]',
    '$..book[?(@.price<=$.expensive)]',
    "$..book[?(@.author ~= '.*Rees')].price",
    '$..*',
]

f = Finder(sample)

for query in queries:
    print(query, f.find(query), '\n')

# You will see a bunch of found items like
# $..book[?(@.author ~= '.*Rees')].price [JsonPathResult(data=8.95, path=Some("$.['store'].['book'][0].['price']"), is_new_value=False)]

JsonPathResult has the following attributes:

  • data: the found value
  • path: the path to the found value
  • is_new_value: whether the value is a new value or a copy of the original value

JsonPathResult can't be constructed from Python; it is only returned by Finder.find().

Caveats

The current implementation is cloning the original PyObject data when converting it to the serde Value. It happens each time you're creating a new Finder instance. Try to reuse the same Finder instance for querying if it's possible.

Also, It has yet another consequence demonstrated in the following example:

>>> original_object_i_want_to_mutate = {'a': {'b': 'sample b'}}
>>> from jsonpath_rust_bindings import Finder
>>> f = Finder(original_object_i_want_to_mutate)
>>> b_dict = f.find('$.a')[0].data
>>> b_dict
{'b': 'sample b'}
>>> b_dict['new'] = 42
>>> original_object_i_want_to_mutate
{'a': {'b': 'sample b'}}

jsonpath-rust-bindings's People

Contributors

night-crawler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.