Skip to main content

Get values from JSON objects using a path expression

Project description

https://travis-ci.org/srittau/python-json-get.svg?branch=master

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


Release history Release notifications

This version
History Node

1.1.1

History Node

1.1.0

History Node

1.0.1

History Node

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
json_get-1.1.1-py3-none-any.whl (6.5 kB) Copy SHA256 hash SHA256 Wheel py3 May 28, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page