Skip to main content

Python bindings for gjson.rs - fast JSON path queries

Project description

pygjson

PyPI - Python Version 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
get(json, path, default) Returns default if path is not found
get_many(json, paths) Query json at each path; returns list[Value]
get_many(json, paths, default) Like get_many but replaces missing values with default
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
v.get(path, default) Sub-query; returns default if not found
v.get_many(paths) Sub-query at multiple paths; returns list[Value]
v.get_many(paths, default) Sub-query at multiple paths; returns list[Value] but replaces missing values with default
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")

# Fetch multiple paths in one call
pygjson.get_many(JSON, ["name.first", "age", "children.1"])
# [Value(Tom), Value(37), Value(Alex)]

# Missing paths return Value(exists=False) without a default …
pygjson.get_many(JSON, ["name.first", "no.such.path"])
# [Value(Tom), Value()]

# … or your chosen default when one is provided
pygjson.get_many(JSON, ["name.first", "no.such.path"], default=None)
# [Value(Tom), None]

# Value.get_many for sub-queries relative to a parsed document
parse(JSON).get_many(["name.first", "name.last"])
# [Value(Tom), Value(Anderson)]

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.4.tar.gz (15.3 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.4-cp310-abi3-win_arm64.whl (220.1 kB view details)

Uploaded CPython 3.10+Windows ARM64

pygjson-0.0.4-cp310-abi3-win_amd64.whl (227.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

pygjson-0.0.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (372.7 kB view details)

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

pygjson-0.0.4-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (370.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

pygjson-0.0.4-cp310-abi3-macosx_11_0_arm64.whl (332.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

pygjson-0.0.4-cp310-abi3-macosx_10_12_x86_64.whl (336.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pygjson-0.0.4.tar.gz
  • Upload date:
  • Size: 15.3 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.4.tar.gz
Algorithm Hash digest
SHA256 fc0b21ac856f7f7fa0047372cb7a2af778796940aa1ea5a5ebb078b92c379967
MD5 8167581fd0ed33e18bb9d649254887c9
BLAKE2b-256 7629fc0cc7a034518a416936b84b4bc2f949faa4af728517b259a61d8bddb1bc

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.4-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 220.1 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.4-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 ebb982d1f9b9fee274537e72a62011df8bc459321b999e3d3e3924b561603d3a
MD5 ec96dba874aa98ee93342648db51ec13
BLAKE2b-256 91a2d10eea89ca475cca7989878a565496cef0dfd217fdaeb2b504b87ebc0be8

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.4-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 227.2 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.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ac1d01a808c9d70a8f0682be0a5a289c90bf93e02f89e5c099482c3fb4df7939
MD5 00e33b9924a38c9a3af236829cf4996e
BLAKE2b-256 0501c5117eb28584c6d8e17da658c2276deceb94586d0bdf38559bf24a4491c0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d6db4c01625ce33b9ce8944c90bb8c3455b092d409ee7ff4125fa0e06724fa27
MD5 448fcc35a6a79597aed8b203c402c793
BLAKE2b-256 d7f77a4df04298e0bc6706dc8d62553bee7e7bb84d0efe643df48a9d492a2b61

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.4-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5d7e21bf38ea79f5afc792f7699c053acbb557a43249758f49aed542fe26a907
MD5 053d300cb5ccacb28e7036da88328e38
BLAKE2b-256 8a9dc58cd41a601eb354126c91ec503fe093f8ea88955b3c94138682bd2a65e4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.4-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 046649c6742133c5e006e39de814c793487ed0d6dfab89f7013e4bffd4b28415
MD5 545f09393c882d6f48a54310f3cf1cf8
BLAKE2b-256 fe8a14ef79e374b8928cfd9fa27ad5c74d32ba0910b52354947794ce0846f706

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.4-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6c69eaf065d38a51e3e3841112baa1945918cc891a0742fff719f8ed20c084ce
MD5 34e99d281f0c57ea143006d1d040a381
BLAKE2b-256 7a0e5884fdb0662c2e1512b416acbe47f482e18cc1dd1f7571fdc60f1d193be9

See more details on using hashes here.

Provenance

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