Skip to main content

Extensions for JSONPath RW

Project description

===============================
python-jsonpath-rw-ext
===============================

.. image:: https://travis-ci.org/sileht/python-jsonpath-rw-ext.png?branch=master
:target: https://travis-ci.org/sileht/python-jsonpath-rw-ext

.. image:: https://img.shields.io/pypi/v/jsonpath-rw-ext.svg
:target: https://pypi-hypernode.com/pypi/jsonpath-rw-ext/
:alt: Latest Version

.. image:: https://img.shields.io/pypi/dm/jsonpath-rw-ext.svg
:target: https://pypi-hypernode.com/pypi/jsonpath-rw-ext/
:alt: Downloads

Extensions for JSONPath RW

jsonpath-rw-ext extends json-path-rw capabilities by adding multiple extensions.
'len' that allows one to get the length of a list. 'sorted' that returns a sorted version
of a list, 'arithmetic' that permits one to make math operation between elements and
'filter' to select only certain elements of a list.

Each extensions will be proposed `upstream <https://github.com/kennknowles/python-jsonpath-rw>`__
and will stay here only if they are refused.

* Free software: Apache license
* Documentation: https://python-jsonpath-rw-ext.readthedocs.org/en/latest/
* Source: http://github.com/sileht/python-jsonpath-rw-ext


Quick Start
-----------

At the command line::

$ pip install jsonpath-rw-ext

Or, if you have virtualenvwrapper installed::

$ mkvirtualenv jsonpath-rw-ext
$ pip install jsonpath-rw-ext


To replace the jsonpath_rw parser by this one with::

import jsonpath_rw_ext
jsonpath_rw_ext.parse("$.foo").find(...)

Or::

from jsonpath_rw_ext import parser
parser.ExtentedJsonPathParser().parse("$.foo").find(...)


The jsonpath classes are not part of the public API, because the name/structure
can change when they will be implemented upstream. Only the syntax *shouldn't*
change.

Extensions
----------

+--------------+----------------------------------------------+
| name | Example |
+==============+==============================================+
| len | - $.objects.`len` |
+--------------+----------------------------------------------+
| sub | - $.field.`sub(/foo\\\\+(.*)/, \\\\1)` |
+--------------+----------------------------------------------+
| split | - $.field.`split(+, 2, -1)` |
| | - $.field.`split(sep, segement, maxsplit)` |
+--------------+----------------------------------------------+
| sorted | - $.objects.`sorted` |
| | - $.objects[\\some_field] |
| | - $.objects[\\some_field,/other_field] |
+--------------+----------------------------------------------+
| filter | - $.objects[?(@some_field > 5)] |
| | - $.objects[?some_field = "foobar")] |
| | - $.objects[?some_field > 5 & other < 2)] |
+--------------+----------------------------------------------+
| arithmetic | - $.foo + "_" + $.bar |
| (-+*/) | - $.foo * 12 |
| | - $.objects[*].cow + $.objects[*].cat |
+--------------+----------------------------------------------+

About arithmetic and string
---------------------------

Operations are done with python operators and allows types that python
allows, and return [] if the operation can be done due to incompatible types.

When operators are used, a jsonpath must be be fully defined otherwise
jsonpath-rw-ext can't known if the expression is a string or a jsonpath field,
in this case it will choice string as type.

Example with data::

{
'cow': 'foo',
'fish': 'bar'
}

| **cow + fish** returns **cowfish**
| **$.cow + $.fish** returns **foobar**
| **$.cow + "_" + $.fish** returns **foo_bar**
| **$.cow + "_" + fish** returns **foo_fish**

About arithmetic and list
-------------------------

Arithmetic can be used against two lists if they have the same size.

Example with data::

{'objects': [
{'cow': 2, 'cat': 3},
{'cow': 4, 'cat': 6}
]}

| **$.objects[\*].cow + $.objects[\*].cat** returns **[6, 9]**

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

jsonpath-rw-ext-0.1.8.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

jsonpath_rw_ext-0.1.8-py2.py3-none-any.whl (16.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jsonpath-rw-ext-0.1.8.tar.gz.

File metadata

File hashes

Hashes for jsonpath-rw-ext-0.1.8.tar.gz
Algorithm Hash digest
SHA256 5fdeaf92d927389e6f6f15b65301d72551818dcca651795ab52a0cd7ec099e1f
MD5 9b3bfc823302cba667f8a273c1c058ae
BLAKE2b-256 3fe0625f56d8f03cb260190128433dcc2659fdf6a563c37f6b5f9a7212627a04

See more details on using hashes here.

Provenance

File details

Details for the file jsonpath_rw_ext-0.1.8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for jsonpath_rw_ext-0.1.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 448d89aa4572b59e5fd1b34b6a9c6a6fe8b1e24f7dad0276c41a35dd0c7d940a
MD5 e2fc56bddfb387202a1fa8f0d443a2a0
BLAKE2b-256 36037a7d4fe92856e61bf2a4a82738c4ec5fcfc1b507a538106c7bd074597b50

See more details on using hashes here.

Provenance

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