jmespaths


Namejmespaths JSON
Version 1.0.1.6 PyPI version JSON
download
home_pagehttps://github.com/jmespath/jmespath.py
SummaryJSON Matching Expressions
upload_time2023-01-30 08:14:03
maintainer
docs_urlNone
authorJames Saryerwinnie
requires_python>=3.7
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            Fork from JMESPath.

New: replace elements in search where they are with value.

No more change.

Original below:


JMESPath
========


.. image:: https://badges.gitter.im/Join Chat.svg
   :target: https://gitter.im/jmespath/chat


JMESPath (pronounced "james path") allows you to declaratively specify how to
extract elements from a JSON document.

For example, given this document::

    {"foo": {"bar": "baz"}}

The jmespath expression ``foo.bar`` will return "baz".

JMESPath also supports:

Referencing elements in a list.  Given the data::

    {"foo": {"bar": ["one", "two"]}}

The expression: ``foo.bar[0]`` will return "one".
You can also reference all the items in a list using the ``*``
syntax::

   {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}

The expression: ``foo.bar[*].name`` will return ["one", "two"].
Negative indexing is also supported (-1 refers to the last element
in the list).  Given the data above, the expression
``foo.bar[-1].name`` will return "two".

The ``*`` can also be used for hash types::

   {"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}

The expression: ``foo.*.name`` will return ["one", "two"].


Installation
============

You can install JMESPath from pypi with:

.. code:: bash

    pip install jmespath


API
===

The ``jmespath.py`` library has two functions
that operate on python data structures.  You can use ``search``
and give it the jmespath expression and the data:

.. code:: python

    >>> import jmespath
    >>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})
    'baz'

Similar to the ``re`` module, you can use the ``compile`` function
to compile the JMESPath expression and use this parsed expression
to perform repeated searches:

.. code:: python

    >>> import jmespath
    >>> expression = jmespath.compile('foo.bar')
    >>> expression.search({'foo': {'bar': 'baz'}})
    'baz'
    >>> expression.search({'foo': {'bar': 'other'}})
    'other'

This is useful if you're going to use the same jmespath expression to
search multiple documents.  This avoids having to reparse the
JMESPath expression each time you search a new document.

Options
-------

You can provide an instance of ``jmespath.Options`` to control how
a JMESPath expression is evaluated.  The most common scenario for
using an ``Options`` instance is if you want to have ordered output
of your dict keys.  To do this you can use either of these options:

.. code:: python

    >>> import jmespath
    >>> jmespath.search('{a: a, b: b}',
    ...                 mydata,
    ...                 jmespath.Options(dict_cls=collections.OrderedDict))


    >>> import jmespath
    >>> parsed = jmespath.compile('{a: a, b: b}')
    >>> parsed.search(mydata,
    ...               jmespath.Options(dict_cls=collections.OrderedDict))


Custom Functions
~~~~~~~~~~~~~~~~

The JMESPath language has numerous
`built-in functions
<http://jmespath.org/specification.html#built-in-functions>`__, but it is
also possible to add your own custom functions.  Keep in mind that
custom function support in jmespath.py is experimental and the API may
change based on feedback.

**If you have a custom function that you've found useful, consider submitting
it to jmespath.site and propose that it be added to the JMESPath language.**
You can submit proposals
`here <https://github.com/jmespath/jmespath.site/issues>`__.

To create custom functions:

* Create a subclass of ``jmespath.functions.Functions``.
* Create a method with the name ``_func_<your function name>``.
* Apply the ``jmespath.functions.signature`` decorator that indicates
  the expected types of the function arguments.
* Provide an instance of your subclass in a ``jmespath.Options`` object.

Below are a few examples:

.. code:: python

    import jmespath
    from jmespath import functions

    # 1. Create a subclass of functions.Functions.
    #    The function.Functions base class has logic
    #    that introspects all of its methods and automatically
    #    registers your custom functions in its function table.
    class CustomFunctions(functions.Functions):

        # 2 and 3.  Create a function that starts with _func_
        # and decorate it with @signature which indicates its
        # expected types.
        # In this example, we're creating a jmespath function
        # called "unique_letters" that accepts a single argument
        # with an expected type "string".
        @functions.signature({'types': ['string']})
        def _func_unique_letters(self, s):
            # Given a string s, return a sorted
            # string of unique letters: 'ccbbadd' ->  'abcd'
            return ''.join(sorted(set(s)))

        # Here's another example.  This is creating
        # a jmespath function called "my_add" that expects
        # two arguments, both of which should be of type number.
        @functions.signature({'types': ['number']}, {'types': ['number']})
        def _func_my_add(self, x, y):
            return x + y

    # 4. Provide an instance of your subclass in a Options object.
    options = jmespath.Options(custom_functions=CustomFunctions())

    # Provide this value to jmespath.search:
    # This will print 3
    print(
        jmespath.search(
            'my_add(`1`, `2`)', {}, options=options)
    )

    # This will print "abcd"
    print(
        jmespath.search(
            'foo.bar | unique_letters(@)',
            {'foo': {'bar': 'ccbbadd'}},
            options=options)
    )

Again, if you come up with useful functions that you think make
sense in the JMESPath language (and make sense to implement in all
JMESPath libraries, not just python), please let us know at
`jmespath.site <https://github.com/jmespath/jmespath.site/issues>`__.


Specification
=============

If you'd like to learn more about the JMESPath language, you can check out
the `JMESPath tutorial <http://jmespath.org/tutorial.html>`__.  Also check
out the `JMESPath examples page <http://jmespath.org/examples.html>`__ for
examples of more complex jmespath queries.

The grammar is specified using ABNF, as described in
`RFC4234 <http://www.ietf.org/rfc/rfc4234.txt>`_.
You can find the most up to date
`grammar for JMESPath here <http://jmespath.org/specification.html#grammar>`__.

You can read the full
`JMESPath specification here <http://jmespath.org/specification.html>`__.


Testing
=======

In addition to the unit tests for the jmespath modules,
there is a ``tests/compliance`` directory that contains
.json files with test cases.  This allows other implementations
to verify they are producing the correct output.  Each json
file is grouped by feature.


Discuss
=======

Join us on our `Gitter channel <https://gitter.im/jmespath/chat>`__
if you want to chat or if you have any questions.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jmespath/jmespath.py",
    "name": "jmespaths",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "James Saryerwinnie",
    "author_email": "js@jamesls.com",
    "download_url": "https://files.pythonhosted.org/packages/eb/1c/56f43b09c0c790a4c4ad54bf6e681b2022c4941ad4f8189c0fefed6f5527/jmespaths-1.0.1.6.tar.gz",
    "platform": null,
    "description": "Fork from JMESPath.\r\n\r\nNew: replace elements in search where they are with value.\r\n\r\nNo more change.\r\n\r\nOriginal below:\r\n\r\n\r\nJMESPath\r\n========\r\n\r\n\r\n.. image:: https://badges.gitter.im/Join Chat.svg\r\n   :target: https://gitter.im/jmespath/chat\r\n\r\n\r\nJMESPath (pronounced \"james path\") allows you to declaratively specify how to\r\nextract elements from a JSON document.\r\n\r\nFor example, given this document::\r\n\r\n    {\"foo\": {\"bar\": \"baz\"}}\r\n\r\nThe jmespath expression ``foo.bar`` will return \"baz\".\r\n\r\nJMESPath also supports:\r\n\r\nReferencing elements in a list.  Given the data::\r\n\r\n    {\"foo\": {\"bar\": [\"one\", \"two\"]}}\r\n\r\nThe expression: ``foo.bar[0]`` will return \"one\".\r\nYou can also reference all the items in a list using the ``*``\r\nsyntax::\r\n\r\n   {\"foo\": {\"bar\": [{\"name\": \"one\"}, {\"name\": \"two\"}]}}\r\n\r\nThe expression: ``foo.bar[*].name`` will return [\"one\", \"two\"].\r\nNegative indexing is also supported (-1 refers to the last element\r\nin the list).  Given the data above, the expression\r\n``foo.bar[-1].name`` will return \"two\".\r\n\r\nThe ``*`` can also be used for hash types::\r\n\r\n   {\"foo\": {\"bar\": {\"name\": \"one\"}, \"baz\": {\"name\": \"two\"}}}\r\n\r\nThe expression: ``foo.*.name`` will return [\"one\", \"two\"].\r\n\r\n\r\nInstallation\r\n============\r\n\r\nYou can install JMESPath from pypi with:\r\n\r\n.. code:: bash\r\n\r\n    pip install jmespath\r\n\r\n\r\nAPI\r\n===\r\n\r\nThe ``jmespath.py`` library has two functions\r\nthat operate on python data structures.  You can use ``search``\r\nand give it the jmespath expression and the data:\r\n\r\n.. code:: python\r\n\r\n    >>> import jmespath\r\n    >>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})\r\n    'baz'\r\n\r\nSimilar to the ``re`` module, you can use the ``compile`` function\r\nto compile the JMESPath expression and use this parsed expression\r\nto perform repeated searches:\r\n\r\n.. code:: python\r\n\r\n    >>> import jmespath\r\n    >>> expression = jmespath.compile('foo.bar')\r\n    >>> expression.search({'foo': {'bar': 'baz'}})\r\n    'baz'\r\n    >>> expression.search({'foo': {'bar': 'other'}})\r\n    'other'\r\n\r\nThis is useful if you're going to use the same jmespath expression to\r\nsearch multiple documents.  This avoids having to reparse the\r\nJMESPath expression each time you search a new document.\r\n\r\nOptions\r\n-------\r\n\r\nYou can provide an instance of ``jmespath.Options`` to control how\r\na JMESPath expression is evaluated.  The most common scenario for\r\nusing an ``Options`` instance is if you want to have ordered output\r\nof your dict keys.  To do this you can use either of these options:\r\n\r\n.. code:: python\r\n\r\n    >>> import jmespath\r\n    >>> jmespath.search('{a: a, b: b}',\r\n    ...                 mydata,\r\n    ...                 jmespath.Options(dict_cls=collections.OrderedDict))\r\n\r\n\r\n    >>> import jmespath\r\n    >>> parsed = jmespath.compile('{a: a, b: b}')\r\n    >>> parsed.search(mydata,\r\n    ...               jmespath.Options(dict_cls=collections.OrderedDict))\r\n\r\n\r\nCustom Functions\r\n~~~~~~~~~~~~~~~~\r\n\r\nThe JMESPath language has numerous\r\n`built-in functions\r\n<http://jmespath.org/specification.html#built-in-functions>`__, but it is\r\nalso possible to add your own custom functions.  Keep in mind that\r\ncustom function support in jmespath.py is experimental and the API may\r\nchange based on feedback.\r\n\r\n**If you have a custom function that you've found useful, consider submitting\r\nit to jmespath.site and propose that it be added to the JMESPath language.**\r\nYou can submit proposals\r\n`here <https://github.com/jmespath/jmespath.site/issues>`__.\r\n\r\nTo create custom functions:\r\n\r\n* Create a subclass of ``jmespath.functions.Functions``.\r\n* Create a method with the name ``_func_<your function name>``.\r\n* Apply the ``jmespath.functions.signature`` decorator that indicates\r\n  the expected types of the function arguments.\r\n* Provide an instance of your subclass in a ``jmespath.Options`` object.\r\n\r\nBelow are a few examples:\r\n\r\n.. code:: python\r\n\r\n    import jmespath\r\n    from jmespath import functions\r\n\r\n    # 1. Create a subclass of functions.Functions.\r\n    #    The function.Functions base class has logic\r\n    #    that introspects all of its methods and automatically\r\n    #    registers your custom functions in its function table.\r\n    class CustomFunctions(functions.Functions):\r\n\r\n        # 2 and 3.  Create a function that starts with _func_\r\n        # and decorate it with @signature which indicates its\r\n        # expected types.\r\n        # In this example, we're creating a jmespath function\r\n        # called \"unique_letters\" that accepts a single argument\r\n        # with an expected type \"string\".\r\n        @functions.signature({'types': ['string']})\r\n        def _func_unique_letters(self, s):\r\n            # Given a string s, return a sorted\r\n            # string of unique letters: 'ccbbadd' ->  'abcd'\r\n            return ''.join(sorted(set(s)))\r\n\r\n        # Here's another example.  This is creating\r\n        # a jmespath function called \"my_add\" that expects\r\n        # two arguments, both of which should be of type number.\r\n        @functions.signature({'types': ['number']}, {'types': ['number']})\r\n        def _func_my_add(self, x, y):\r\n            return x + y\r\n\r\n    # 4. Provide an instance of your subclass in a Options object.\r\n    options = jmespath.Options(custom_functions=CustomFunctions())\r\n\r\n    # Provide this value to jmespath.search:\r\n    # This will print 3\r\n    print(\r\n        jmespath.search(\r\n            'my_add(`1`, `2`)', {}, options=options)\r\n    )\r\n\r\n    # This will print \"abcd\"\r\n    print(\r\n        jmespath.search(\r\n            'foo.bar | unique_letters(@)',\r\n            {'foo': {'bar': 'ccbbadd'}},\r\n            options=options)\r\n    )\r\n\r\nAgain, if you come up with useful functions that you think make\r\nsense in the JMESPath language (and make sense to implement in all\r\nJMESPath libraries, not just python), please let us know at\r\n`jmespath.site <https://github.com/jmespath/jmespath.site/issues>`__.\r\n\r\n\r\nSpecification\r\n=============\r\n\r\nIf you'd like to learn more about the JMESPath language, you can check out\r\nthe `JMESPath tutorial <http://jmespath.org/tutorial.html>`__.  Also check\r\nout the `JMESPath examples page <http://jmespath.org/examples.html>`__ for\r\nexamples of more complex jmespath queries.\r\n\r\nThe grammar is specified using ABNF, as described in\r\n`RFC4234 <http://www.ietf.org/rfc/rfc4234.txt>`_.\r\nYou can find the most up to date\r\n`grammar for JMESPath here <http://jmespath.org/specification.html#grammar>`__.\r\n\r\nYou can read the full\r\n`JMESPath specification here <http://jmespath.org/specification.html>`__.\r\n\r\n\r\nTesting\r\n=======\r\n\r\nIn addition to the unit tests for the jmespath modules,\r\nthere is a ``tests/compliance`` directory that contains\r\n.json files with test cases.  This allows other implementations\r\nto verify they are producing the correct output.  Each json\r\nfile is grouped by feature.\r\n\r\n\r\nDiscuss\r\n=======\r\n\r\nJoin us on our `Gitter channel <https://gitter.im/jmespath/chat>`__\r\nif you want to chat or if you have any questions.\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "JSON Matching Expressions",
    "version": "1.0.1.6",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cde6061f96a47eaab9b69705b04e890498ec1013756c0623edf07fd63fa65855",
                "md5": "d792fdf516477c08b01635443c71985f",
                "sha256": "c5a12f8ff0d8fd6fb0ea480166a879d13d3a827abd03b0b55c1741310c4723b8"
            },
            "downloads": -1,
            "filename": "jmespaths-1.0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d792fdf516477c08b01635443c71985f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 21726,
            "upload_time": "2023-01-30T08:14:02",
            "upload_time_iso_8601": "2023-01-30T08:14:02.023786Z",
            "url": "https://files.pythonhosted.org/packages/cd/e6/061f96a47eaab9b69705b04e890498ec1013756c0623edf07fd63fa65855/jmespaths-1.0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb1c56f43b09c0c790a4c4ad54bf6e681b2022c4941ad4f8189c0fefed6f5527",
                "md5": "d28e5a954b0565347d69ffebdf7de813",
                "sha256": "3bc616cd2523a10daad820b616cb1250cb5c5e1ac8d897e9e154533ab14651be"
            },
            "downloads": -1,
            "filename": "jmespaths-1.0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "d28e5a954b0565347d69ffebdf7de813",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 21845,
            "upload_time": "2023-01-30T08:14:03",
            "upload_time_iso_8601": "2023-01-30T08:14:03.823258Z",
            "url": "https://files.pythonhosted.org/packages/eb/1c/56f43b09c0c790a4c4ad54bf6e681b2022c4941ad4f8189c0fefed6f5527/jmespaths-1.0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-30 08:14:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "jmespath",
    "github_project": "jmespath.py",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "jmespaths"
}
        
Elapsed time: 0.04005s