Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/srittau/python-json-get
Get values from JSON objects using a path expression
https://github.com/srittau/python-json-get
Last synced: about 1 month ago
JSON representation
Get values from JSON objects using a path expression
- Host: GitHub
- URL: https://github.com/srittau/python-json-get
- Owner: srittau
- License: mit
- Created: 2017-03-19T21:26:09.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-06-26T12:12:33.000Z (over 1 year ago)
- Last Synced: 2024-10-08T22:01:19.820Z (about 1 month ago)
- Language: Python
- Size: 151 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
Python JSON Get
===============.. image:: https://img.shields.io/pypi/l/json-get.svg
:target: https://pypi.python.org/pypi/json-get/
.. image:: https://img.shields.io/github/release/srittau/python-json-get/all.svg
:target: https://github.com/srittau/python-json-get/releases/
.. image:: https://img.shields.io/pypi/v/json-get.svg
:target: https://pypi.python.org/pypi/json-get/
.. image:: https://img.shields.io/github/workflow/status/srittau/python-json-get/Test%20and%20lint
:target: https://github.com/srittau/python-json-get/actionsGet values from JSON objects usings a path expression. Optional type
checking is possible:>>> from jsonget import json_get, json_get_default, JList
>>> j = {
... "foo": {"num": 3.4, "s": "Text"},
... "arr": [10, 20, 30],
... }
>>> json_get(j, "/foo/num")
3.4
>>> json_get(j, "/arr[1]")
20
>>> json_get(j, "/foo/unknown")
Traceback (most recent call last):
...
ValueError: JSON path '/foo/unknown' not foundValues are optionally checked against one of the following types:
``str``, ``int``, ``float``, ``bool``, ``list``, and ``dict``.
Checking for null values is not supported:>>> json_get(j, "/foo/num", str)
Traceback (most recent call last):
...
TypeError: wrong JSON type str != float``float`` will match any number, ``int`` will only match numbers without
a fractional part:>>> json_get(j, "/foo/num", float)
3.4
>>> json_get(j, "/foo/num", int)
Traceback (most recent call last):
...
TypeError: wrong JSON type int != floatAdditionally, the type of list values can be checked:
>>> json_get(j, "/arr", JList(int))
[10, 20, 30]``json_get_default()`` can be used to return a default value if a given
path does not exist:>>> json_get_default(j, "/bar", "default value")
'default value'