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

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

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

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 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)]

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

Uploaded CPython 3.10+Windows ARM64

pygjson-0.0.7-cp310-abi3-win_amd64.whl (238.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

pygjson-0.0.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (387.2 kB view details)

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

pygjson-0.0.7-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (381.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

pygjson-0.0.7-cp310-abi3-macosx_11_0_arm64.whl (342.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

pygjson-0.0.7-cp310-abi3-macosx_10_12_x86_64.whl (349.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pygjson-0.0.7.tar.gz
  • Upload date:
  • Size: 24.8 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.7.tar.gz
Algorithm Hash digest
SHA256 c87d23a33fc9e1f0d2bb2c8c8c4fdbf66d6b13fc6f1d6d1b6bb359d86675f94e
MD5 dd3452887b47b557a2d38d128775b4b2
BLAKE2b-256 ed5ba29e4cc4075c99aabd0149d3b351d06a4018ffab1a42e31ef32a60193550

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.7-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 231.2 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.7-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 b9b22b08d401a28dfc5f6dfba1c1253e82ba0859edea2a27d32444293c0c4933
MD5 072ee881f663fa1b7334d6a5dbb23c47
BLAKE2b-256 36608c322a33feeceff1f3f7b7cdcb1076d10df276de220b49c02c9edf4b28c3

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.7-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 238.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.7-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0533e9a83fb32b885e6d8b051c6d4f3438fbdfcfb26ea89670fd702dffc189f1
MD5 0f91c1d15d689053495f50dcadab3b6d
BLAKE2b-256 7b3b936639f7b5f22013d597b031229810f17e18bd27e14c20f1c56d11b063de

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9548a67fa6aa4b1ec46d3ea249ca5d47a36d1bb7a03e51c7408301bbc7913bda
MD5 edc6645850ce779d4f5a0c6c762d5312
BLAKE2b-256 d8ac67781b438f43d6493f3313c1c6ec918e84be2e420ddf7d49bb085ae5bfbf

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.7-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c225dfe95201c93029456d08a31360f77cea25bb3051e1e99754b346407a04e1
MD5 7a40f7880a921c8661b180c109afdd3f
BLAKE2b-256 a3047d1d574d5d80e771136897f651fa301206478a9a0d66d95abb8b0bef950d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.7-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a0fb375f2b506f5d33d73cb6ef31b70b09ac31e81f9eaef3d1477000c684e8d5
MD5 7e557a9bc5aa159124e77b4c0a413b36
BLAKE2b-256 f2e6ca1c81a7c67baf50d29440c692c9f6eac9c935edf8d905f8de3b4ca72afc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.7-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1b97e13d0eb34935abc6e59a120eae89c14b85c63dc77931efbc79aee7581125
MD5 2bafb0f78760670443838e9103832327
BLAKE2b-256 47d7b02b36c3b9048cc7eaeceddeed2f72168de49e12420b0e8e02dc25c4ed10

See more details on using hashes here.

Provenance

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