Get values from JSON objects using a path expression
Project description
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_get-1.1.1-py3-none-any.whl.
File metadata
- Download URL: json_get-1.1.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
884854fb225aa72e2a975dc545704f93e1f87f9377108c27fa27b0c0cca92374
|
|
| MD5 |
dd28c7aef2c6e6d12d454389f6ffc064
|
|
| BLAKE2b-256 |
fef6a8fe09ed8b3db17ceb41ae21e54923256402c3fca90d8874fe9216721ae9
|