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(r) for r in pygjson.get(JSON, "children|@reverse")]
# ['Jack', 'Alex', 'Sara']

pygjson.validate(JSON)  # True

p = pygjson.compile("name.first")
str(pygjson.get(JSON, p))  # 'Tom'  — reuse the compiled path

API

Module-level functions

Function Description
get(json, path) Query json (str) at path; returns Result
get_bytes(json, path) Query json (bytes) at path; returns Result
get_many(json, paths) Query json (str) at each path; returns list[Result]
get_many_bytes(json, paths) Query json (bytes) at each path; returns list[Result]
parse(json) Parse the entire JSON document into a Result
validate(json) True if json is syntactically valid
compile(path) Pre-compile a path string into a Path; returns Path

get_bytes and get_many_bytes raise UnicodeDecodeError if json is not valid UTF-8.

All query functions (get, get_bytes, get_many, get_many_bytes) and Result.get / Result.get_many accept either a plain str or a compiled Path as the path argument.

Path

compile(path) parses a path string once and returns a Path object that can be reused across multiple queries. Pass a Path anywhere a path string is accepted.

Property / Method Description
repr(p) Path("name.first") — shows the path

Result

get and parse return a Result. Result accessors

Properties

Property Description
r.type_ Python type for this value: None, bool, int, float, str, list, dict
r.value Value converted to the corresponding Python type: None / int / float / str / list[Result] / dict[str, Result]

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

Method Description
r.exists() True if the value was found in the JSON
r.to_str() String representation (gjson str behaviour)
r.to_int() Integer (i64 for negative, u64 for non-negative)
r.to_float() 64-bit float
r.to_bool() True only for the JSON literal true
r.get(path) Sub-query relative to this value
r.get_many(paths) Sub-query at multiple paths; returns list[Result]

Pythonic methods — follow standard Python protocols:

Syntax Description
str(r),repr(r) dict: <Result type=dict, keys=[...]>; list: <Result type=list, value=[...]>; others: str(r.value)
int(r) Integer (negative → i64, non-negative → u64)
float(r) 64-bit float
bool(r) Equivalent to bool(r.value)False for null/false/0/""/[]/{}
len(r) Chars for String; element count for Array/Object
r[key] Subscript access
key in r Key membership for Object; string match for Array elements
iter(r) Lazy iterator: chars for String; Results for Array; keys for Object
r.keys() Lazy KeysView of object keys (raises TypeError for non-Object)
r.values() Lazy ValuesView of object values (raises TypeError for non-Object)
r.items() Lazy ItemsView of (key, Result) pairs (raises TypeError for non-Object)

Lazy iteration

iter(r), r.keys(), r.values() and r.items() all return lightweight lazy objects rather than fully-materialised lists, mirroring Python's built-in dict_keys / dict_values / dict_items.

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

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

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

If you need a fully materialised collection, wrap the view with list(...) or dict(...) explicitly, or use r.value to get a native Python object.

Usage examples

from pygjson import compile, get, get_bytes, get_many, get_many_bytes, parse, validate

# Missing value returns Result(exists=False)
r = get(JSON, "no.such.path")
r.exists()   # False
bool(r)      # False (null → bool(None) = False)

# Type conversion
age = get(JSON, "age")
age.to_int()    # gjson i64/u64 behaviour
int(age)        # Python int protocol
age.type_       # <class 'int'>
age.value       # 37

# Boolean distinction
get('{"flag": true}', "flag").to_bool()   # True  (JSON true literal)
bool(get(JSON, "age"))                    # True  (37 is truthy)
bool(get('{"n": 0}', "n"))               # False (0 is falsy)

# Bytes input
get_bytes(JSON.encode(), "name.first")    # Result("Tom")

# Array iteration and subscript
children = get(JSON, "children")
list(children)                  # [Result("Sara"), Result("Alex"), Result("Jack")]
[str(r) for r in children]      # ['Sara', 'Alex', 'Jack']
"Sara" in children              # True
str(children[0])                # 'Sara'
str(children[-1])               # 'Jack'
[str(r) for r in children[1:]] # ['Alex', 'Jack']

# String subscript
first = get(JSON, "name.first")   # "Tom"
first[0]                          # 'T'
first[-1]                         # 'm'
first[1:]                         # 'om'
first[::-1]                       # 'moT'

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

# repr
repr(name)                      # '<Result type=dict, keys=["first", "last"]>'
repr(children)                  # '<Result type=list, value=["Sara", "Alex", ...]>'
repr(age)                       # '37'
repr(first)                     # 'Tom'

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

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

# Missing paths return Result(exists=False)
get_many(JSON, ["name.first", "no.such.path"])
# [Result(Tom), Result()]

# get_many with bytes input
get_many_bytes(JSON.encode(), ["name.first", "age"])
# [Result(Tom), Result(37)]

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

# Pre-compile a path for reuse across multiple queries
p = compile("name.first")
get(JSON, p)                    # Result(Tom)
get_bytes(JSON.encode(), p)     # Result(Tom)

# Compiled paths work with get_many / get_many_bytes
paths = [compile("name.first"), compile("age"), compile("children.1")]
get_many(JSON, paths)
# [Result(Tom), Result(37), Result(Alex)]

# Compiled paths work with Result.get / Result.get_many
root = parse(JSON)
root.get(compile("name.first"))
# Result(Tom)
root.get_many([compile("name.first"), compile("name.last")])
# [Result(Tom), Result(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.9.tar.gz (26.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.9-cp310-abi3-win_arm64.whl (243.6 kB view details)

Uploaded CPython 3.10+Windows ARM64

pygjson-0.0.9-cp310-abi3-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.10+Windows x86-64

pygjson-0.0.9-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (403.7 kB view details)

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

pygjson-0.0.9-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (399.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

pygjson-0.0.9-cp310-abi3-macosx_11_0_arm64.whl (358.5 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

pygjson-0.0.9-cp310-abi3-macosx_10_12_x86_64.whl (364.5 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pygjson-0.0.9.tar.gz
  • Upload date:
  • Size: 26.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.9.tar.gz
Algorithm Hash digest
SHA256 e1e03f856ffe3c0c17ffad82670e99a53035d00bfb5a684f48e680befed5bf01
MD5 0682a9091f25d28caf611aa1f2207bda
BLAKE2b-256 168534dbe1b77c8fafefff5b75b1d1df8394bc78e89d80830928a8d6001eaef5

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.9-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 243.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.9-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 fab8a96c1153da6f60fcb071b565da2799cbb28c2ebbd4b785ba5ed5ea5316d2
MD5 533f529147835227f24e908e517f97a3
BLAKE2b-256 98f85290334ce8ed80c502dd330bd120ce5f7f6af66a9bfcfdbc820c9c2997c5

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.9-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.9-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d8f4fb1d3b32cc33503007aaf92d40424bbd24ec62396b725d57d3f6c9491123
MD5 8945d93e2d4081308b4d0a41b18797e4
BLAKE2b-256 2cdd840d664ca2f42e87877a60b1f14a87a3956c344ea5e3389dab46037ee022

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.9-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8e6dde5b72ca20ed5aeef26914b35d585912c9c34bb907f4e80a4bd3322e14d0
MD5 5758668d4d5fd6e6eb011b1935d9fb0b
BLAKE2b-256 185fefcf42b6df877e1017c7bee78c1449ec1e24929958a9164b8c9cf3c64c39

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.9-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 16fd3b6a6722cfb68a0d3aaa6f308f6c98c08aef8157d81277cf5e02d5a7aa38
MD5 b0e518faf97a61dddeb817fd9493d188
BLAKE2b-256 c294134c79efe703a612e5bf9a1298d64193a87e1dd9317bc6668d338ff2e20b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.9-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0250c6e205c62a25c55cbd3f243e7299a2dc2a4a465221a814f8dfb8c5937cc
MD5 031e850300c084d923f759eeacba4b34
BLAKE2b-256 322e5dea785aae367e24a1ec78b2e590961f92238ffbd83061011af2ae9525be

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.9-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 653dc877e617ed7f73ad7e6397b285272110af1fa6380a8038bbef01bc3a04a5
MD5 2f23c222d369dfda95ba677d7331b3ad
BLAKE2b-256 6338440abdaa22159e4fab10edaca457210d92143d16bc978103408da855b9ee

See more details on using hashes here.

Provenance

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