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.3.tar.gz (14.2 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.3-cp310-abi3-win_arm64.whl (213.0 kB view details)

Uploaded CPython 3.10+Windows ARM64

pygjson-0.0.3-cp310-abi3-win_amd64.whl (218.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

pygjson-0.0.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (364.3 kB view details)

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

pygjson-0.0.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (360.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

pygjson-0.0.3-cp310-abi3-macosx_11_0_arm64.whl (325.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

pygjson-0.0.3-cp310-abi3-macosx_10_12_x86_64.whl (332.8 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pygjson-0.0.3.tar.gz
  • Upload date:
  • Size: 14.2 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.3.tar.gz
Algorithm Hash digest
SHA256 f8c8634fb1831fbe67f4575700d3034d480ea5e3f13347bd3982f723f990e730
MD5 cfafa86b5d2197ca0a3cb9cc41a48156
BLAKE2b-256 bad801cd389ff617d6216ebd3f69676214343473be6c60955a04dff096ee3cbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3.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.3-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: pygjson-0.0.3-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 213.0 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.3-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 c5a7079e0e8145da079c64f3d55cc9e7770607562718d109914c40bd0e031673
MD5 503b9e517dbbbf559e3637bee562a3a9
BLAKE2b-256 1f688f37928db2188a341d6b8a0dbabb9e6b0ef3980ca698eb816c1df3b8ef49

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3-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.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: pygjson-0.0.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 218.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.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9b164004eac4e9fe1e51b0e51680dc96777eca0111c861dce8c51faebfc178e2
MD5 a927a5025c9f663a176791d017182e9d
BLAKE2b-256 8591bdad9401636006f2ec10e022b33d2504e15df33d9bcf5c231017a8686955

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3-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.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0ae90b942845cf65c20e4b8f113a9a8ed1b2746a6b85cd77623960d2720873e1
MD5 29541c52c50d4c12273f87d299f9e49f
BLAKE2b-256 16c45b993d9db6608d44af14564cc4b72d3960e47248979f13f47c9e83bb56f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3-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.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 206d9acf3538ce0280b5a8c90f75723ac93fb183315b8b9f72f8e1fe58f8e8b3
MD5 c6507087c9883b5b20c2f62023b99d87
BLAKE2b-256 df34aa995760ca68f688caf508154810294f07559b4b66a4d4f7f2112a708692

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3-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.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7032622cd17f1dac9f1663926cd2c555cd399f937a2d591de4b45c0713bfc58f
MD5 6f88386c229237c93d8f740b31f62f7e
BLAKE2b-256 28e8c6fddeb6d8f43234a34acb18b1eafadf2d00d785ef69a2a81d3eb900f212

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3-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.3-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pygjson-0.0.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 52bc5a7b6ef4d3a326d72be642a39de61186b4d76ac582b4e3d84e1654d7b925
MD5 95a667a506fbb72592644f3e85dacacf
BLAKE2b-256 1c3c5324e8195e6b8c8af497c0e473fe20d09ade55e11fe819892c0ba5bae709

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygjson-0.0.3-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