gjson-py is a Python package that provides a simple way to filter and extract data from JSON-like objects or JSON files, using the GJSON syntax.
Project description
Introduction
gjson-py is a Python package that provides a simple way to filter and extract data from JSON-like objects or JSON files, using the GJSON syntax.
It is, compatibly with the language differences and with some limitation, the Python equivalent of the Go GJSON package. The main difference from GJSON is that gjson-py doesn’t work directly with JSON strings but instead with JSON-like Python objects, that can either be the resulting object when calling json.load() or json.loads(), or any Python object that is JSON-serializable.
A detailed list of the GJSON features supported by gjson-py is provided below.
See also the full gjson-py documentation.
Installation
gjson-py is available on the Python Package Index (PyPI) and can be easily installed with:
pip install gjson
How to use the library
gjson-py provides different ways to perform queries on JSON-like objects.
gjson.get()
A quick accessor to GJSON functionalities exposed for simplicity of use. Particularly useful to perform a single query on a given object:
>>> import gjson >>> data = {'name': {'first': 'Tom', 'last': 'Anderson'}, 'age': 37} >>> gjson.get(data, 'name.first') 'Tom'
It’s also possible to make it return a JSON-encoded string and decide on failure if it should raise an exception or return None. See the full API documentation for more details.
GJSON class
The GJSON class provides full access to the gjson-py API allowing to perform multiple queries on the same object:
>>> import gjson >>> data = {'name': {'first': 'Tom', 'last': 'Anderson'}, 'age': 37} >>> source = gjson.GJSON(data) >>> source.get('name.first') 'Tom' >>> str(source) '{"name": {"first": "Tom", "last": "Anderson"}, "age": 37}' >>> source.getj('name.first') '"Tom"' >>> name = source.get_gjson('name') >>> name.get('first') 'Tom' >>> name <gjson.GJSON object at 0x102735b20>
See the full API documentation for more details.
Query syntax
For the generic query syntax refer to the original GJSON Path Syntax documentation.
Supported GJSON features
This is the list of GJSON features and how they are supported by gjson-py:
GJSON feature |
Supported by gjson-py |
Notes |
---|---|---|
YES |
||
YES |
||
YES |
||
YES |
||
YES |
||
PARTIALLY |
Subqueries and the tilde operator are not supported |
|
YES |
||
PARTIALLY |
See the table below |
|
YES |
Only a JSON object is accepted as argument |
|
YES |
Only a JSON object is accepted as argument |
|
NO |
||
NO |
||
YES |
This is the list of modifiers and how they are supported by gjson-py:
GJSON Modifier |
Supported by gjson-py |
Notes |
@reverse |
YES |
|
@ugly |
YES |
|
@pretty |
PARTIALLY |
The width argument is not supported |
@this |
NO |
|
@valid |
YES |
|
@flatten |
YES |
|
@join |
NO |
|
@keys |
YES |
|
@values |
YES |
|
@tostr |
NO |
|
@fromstr |
NO |
|
@group |
NO |
|
@sort |
YES |
Not present in GJSON |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.