Get values from JSON objects using a path expression
Project description
Python JSON Get
===============
.. image:: https://img.shields.io/pypi/l/json-get.svg
:target: https://pypi-hypernode.com/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-hypernode.com/pypi/json-get/
.. image:: https://travis-ci.org/srittau/python-json-get.svg?branch=master
:target: https://travis-ci.org/srittau/python-json-get
Get 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 found
Values 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 != float
Additionally, 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'
===============
.. image:: https://img.shields.io/pypi/l/json-get.svg
:target: https://pypi-hypernode.com/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-hypernode.com/pypi/json-get/
.. image:: https://travis-ci.org/srittau/python-json-get.svg?branch=master
:target: https://travis-ci.org/srittau/python-json-get
Get 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 found
Values 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 != float
Additionally, 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'
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file json_get-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: json_get-1.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 098c2dbbc4ed24f9f0238025587b8b6d067c6ce0b01e754bd2e4180e29d5a044 |
|
MD5 | b42a288984793c7935ba3d0d42239bfc |
|
BLAKE2b-256 | 7bd1f293a7352d1f71e320511955d1a1c62f86f2c334135fa74c71c3944970fa |