Skip to main content

Python bindings for gjson.rs - fast JSON path queries

Project description

pygjson

PyPI - Version GitHub License

PYGJSON is a Python binding for tidwall/gjson.rs - fast JSON path queries.

The original GJSON: tidwall/gjson

Installation

pip install pygjson

Quick example

import pygjson

JSON = """{
  "name": {"first": "Tom", "last": "Anderson"},
  "age": 37,
  "children": ["Sara", "Alex", "Jack"],
  "friends": [
    {"first": "Dale",  "last": "Murphy", "age": 44},
    {"first": "Roger", "last": "Craig",  "age": 68},
    {"first": "Jane",  "last": "Murphy", "age": 47}
  ]
}"""

str(pygjson.get(JSON, "name.last"))                           # 'Anderson'
int(pygjson.get(JSON, "age"))                                 # 37
int(pygjson.get(JSON, "children.#"))                         # 3
str(pygjson.get(JSON, "children.1"))                          # 'Alex'
str(pygjson.get(JSON, 'friends.#(last="Murphy").first'))      # 'Dale'

[str(v) for v in pygjson.get(JSON, "children|@reverse")]
# ['Jack', 'Alex', 'Sara']

pygjson.valid(JSON)  # True

API

Module-level functions

Function Description
get(json, path) Query json at path; returns Value (gjson-native)
get(json, path, default) Returns default if path is not found (Pythonic)
parse(json) Parse the entire JSON document into a Value
valid(json) True if json is syntactically valid

Value

get and parse return a Value. The API is split into two layers:

gjson-native methods — mirror the Rust gjson::Value API directly:

Method Description
v.kind() Returns a Kind enum value
v.exists() True if the value was actually found in the JSON
v.to_str() String representation (gjson str behaviour)
v.to_int() Signed 64-bit integer (i64)
v.to_uint() Unsigned 64-bit integer (u64)
v.to_float() 64-bit float
v.to_bool() True only for the JSON literal true
v.json() Raw JSON text for this value
v.get(path) Sub-query relative to this value (gjson-native)
v.get(path, default) Sub-query; returns default if not found (Pythonic)
v.to_list() list[Value] for arrays
v.to_dict() dict[str, Value] for objects

Pythonic methods — follow standard Python protocols:

Syntax Description
str(v) String representation
int(v) Integer (negative → i64, non-negative → u64)
float(v) 64-bit float
bool(v) True if v.exists()
len(v) Chars for String; element count for Array/Object
v[key] Subscript access for Object values
key in v Key membership for Object; str match for Array elements
iter(v) Lazy iterator: chars for String; Values for Array; keys for Object
v.keys() Lazy KeysView of object keys (raises TypeError for non-Object)
v.values() Lazy ValuesView of object values (raises TypeError for non-Object)
v.items() Lazy ItemsView of (key, Value) pairs (raises TypeError for non-Object)

Lazy iteration

iter(v), v.keys(), v.values() and v.items() all return lightweight lazy objects rather than fully-materialised lists, mirroring Python's built-in dict_keys / dict_values / dict_items. Only one child wrapper is alive at any time, so iterating a large array or object never pays the cost of allocating one Python object per element up front.

v = parse('{"a": 1, "b": 2, "c": 3}')

ks = v.keys()           # KeysView(["a", "b", "c"]) — no materialisation yet
len(ks)                 # 3
"a" in ks               # True
list(ks)                # ['a', 'b', 'c']

for k, child in v.items():   # ItemsView, lazily yields one pair at a time
    ...

If you need a fully materialised collection (for example to keep references to every child), call v.to_list() / v.to_dict() or wrap the view with list(...) / dict(...) explicitly.

Kind

from pygjson import Kind
Kind.Null   Kind.False_   Kind.True_   Kind.Number
Kind.String Kind.Array    Kind.Object

(False and True are Python keywords, so the variants are named with a trailing underscore.)

Usage examples

from pygjson import get, parse, valid, Kind

# gjson-native: missing value returns Value(exists=False)
v = get(JSON, "no.such.path")
v.exists()   # False
bool(v)      # False

# Pythonic: missing value returns None (or a custom default)
get(JSON, "no.such.path", None)   # None
get(JSON, "no.such.path", 42)     # 42

# Type conversion
age = get(JSON, "age")
age.to_int()    # gjson i64 behaviour
int(age)        # Python int protocol

# Boolean distinction
get('{"flag": true}', "flag").to_bool()    # True  (JSON true literal)
bool(get(JSON, "age"))                     # True  (value exists)

# Array iteration
children = get(JSON, "children")
list(children)                  # [Value("Sara"), Value("Alex"), Value("Jack")]
[str(v) for v in children]      # ['Sara', 'Alex', 'Jack']
"Sara" in children              # True

# Object (dict-like) access
name = get(JSON, "name")
name["first"]                   # Value("Tom")
"first" in name                 # True
list(name)                      # ['first', 'last']  — keys
dict(name)                      # {'first': Value("Tom"), 'last': Value("Anderson")}
for k, v in name.items():
    print(k, str(v))

# Chained queries
parse(JSON).get("name").get("first")   # Value("Tom")

Path syntax

For the full path / query / modifier syntax see the upstream gjson.rs and the original GJSON path syntax.

License

MIT

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

pygjson-0.0.2.tar.gz (14.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pygjson-0.0.2-cp310-abi3-win_arm64.whl (208.6 kB view details)

Uploaded CPython 3.10+Windows ARM64

pygjson-0.0.2-cp310-abi3-win_amd64.whl (217.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

pygjson-0.0.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (359.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

pygjson-0.0.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (357.6 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

pygjson-0.0.2-cp310-abi3-macosx_11_0_arm64.whl (317.4 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

pygjson-0.0.2-cp310-abi3-macosx_10_12_x86_64.whl (323.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file pygjson-0.0.2.tar.gz.

File metadata

  • Download URL: pygjson-0.0.2.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygjson-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6e8df4afaf29c8c923ce32fdeaa76e8da79a50945f346b59a20ec7e99112b47e
MD5 3ee1e7380626c7b0723f5048ea66379f
BLAKE2b-256 9bbdf73248ad1c8d0f8723cbdd64b5ae1efe9516146b3740cb04d3dc3294bbad

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2.tar.gz:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygjson-0.0.2-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: pygjson-0.0.2-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 208.6 kB
  • Tags: CPython 3.10+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygjson-0.0.2-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 d3aeb3a36513d7f7eaccf77343d322de74de306ec7b4975703bb7a8b593387d7
MD5 8dc43e2af1a65cb130b512ce62719ea9
BLAKE2b-256 38df3497bca037a703f145567bdfa18b614e134f18b96c36f6e0d30df419892a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2-cp310-abi3-win_arm64.whl:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygjson-0.0.2-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: pygjson-0.0.2-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 217.9 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygjson-0.0.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 28ea99b52ad93d9c4a70e4c5b68741758df5e2136a20551fea56effbbf543b06
MD5 d1d1b82078b54fd80877ec2b8998e11d
BLAKE2b-256 bb501c8034e63bbef1ff6af032a14b6ed07bf3ff68485b7bb504842edff6dab4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2-cp310-abi3-win_amd64.whl:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygjson-0.0.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48fcffb40933cad25afbcf6be60bb3ff9493a2fc2d30df53156142717b6f16a5
MD5 27b7af4113898735ceb7ff5315fde6fc
BLAKE2b-256 d8b47d5b1da282aaf6bec19a39a8c26a4ae7f162f2a98a5e79e8eea857f1da7d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygjson-0.0.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 efbd1adb9b5fe596de809df571cb6b05e74b72f9e37a202e23bfcb970cd3d5df
MD5 2e4643e15c07452ff1e265156f37e545
BLAKE2b-256 c4fcf9004e36830498cc19c89297df0001942c9ce25448401ae45c7125342c5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygjson-0.0.2-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1bb9adf979dfa3f7864fe8416e29fc550214835112f64e62d856144269b08857
MD5 6598b3eb6e3a845c2f14e2dc933cf2a8
BLAKE2b-256 acd5daac1e0ee18cf51b676dddcc7f29cf6926fd15d8e110956a77f1058f3e35

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygjson-0.0.2-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9deeccf587068478b5768a5c6c0a02c365c8dbee1870e2357ce491a12f00c58c
MD5 b114fed0ac1cc3783ce96e213fcd50e9
BLAKE2b-256 2dde6028e35c6f3c56708380b23117179adad6f54b7d66b26eb19c9f0c44327f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.2-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: publish.yml on minefuto/pygjson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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