Skip to main content

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

CI results

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

Path Structure

YES

Basic

YES

Wildcards

YES

Escape Character

YES

Arrays

YES

Queries

PARTIALLY

Subqueries and the tilde operator are not supported

Dot vs Pipe

YES

Modifiers

PARTIALLY

See the table below

Modifier arguments

YES

Custom modifier

NO

Multipaths

NO

Literals

NO

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


Download files

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

Source Distribution

gjson-0.0.2.tar.gz (32.1 kB view hashes)

Uploaded Source

Built Distribution

gjson-0.0.2-py3-none-any.whl (21.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page