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.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. The API is split into two layers:

Properties

Property Description
v.type_ Python type for this value: None, bool, int, float, str, list, dict
v.value Value converted to the corresponding Python type

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

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

Pythonic methods — follow standard Python protocols:

Syntax Description
str(v),repr(v) dict: <Result type=dict, keys=[...]>; list: <Result type=list, value=[...]>; others: str(v.value)
int(v) Integer (negative → i64, non-negative → u64)
float(v) 64-bit float
bool(v) Equivalent to bool(v.value)False for null/false/0/""/[]/{}
len(v) Chars for String; element count for Array/Object
v[key] Subscript access — see table below
key in v Key membership for Object; string match for Array elements
iter(v) Lazy iterator: chars for String; Results 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, Result) pairs (raises TypeError for non-Object)

Subscript access v[key]

Value type key type Result
String int Nth code point as str (negative indexing supported)
String slice Substring as str
String str TypeError
Array int Result at that index (negative indexing supported)
Array slice New Result of type Array with selected elements
Array str TypeError
Object str Result at that key
Object int/slice KeyError
Null int IndexError
Null slice Empty Result (exists() is False)
Null str TypeError

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.

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

ks = v.keys()           # KeysView — 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, wrap the view with list(...) or dict(...) explicitly, or use v.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)
v = get(JSON, "no.such.path")
v.exists()   # False
bool(v)      # 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(v) for v in children]      # ['Sara', 'Alex', 'Jack']
"Sara" in children              # True
str(children[0])                # 'Sara'
str(children[-1])               # 'Jack'
[str(v) for v 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.5.tar.gz (25.0 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.5-cp310-abi3-win_arm64.whl (224.2 kB view details)

Uploaded CPython 3.10+Windows ARM64

pygjson-0.0.5-cp310-abi3-win_amd64.whl (231.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

pygjson-0.0.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (377.5 kB view details)

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

pygjson-0.0.5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (374.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

pygjson-0.0.5-cp310-abi3-macosx_11_0_arm64.whl (335.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

pygjson-0.0.5-cp310-abi3-macosx_10_12_x86_64.whl (340.8 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pygjson-0.0.5.tar.gz
  • Upload date:
  • Size: 25.0 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.5.tar.gz
Algorithm Hash digest
SHA256 f604a1642ce9685a48bcc06c922cf95e5cd3f3154139666f3132177b12fe396f
MD5 a1aef5d76deef15e9c08b3109428619d
BLAKE2b-256 d0aedb9b47db9b946f31961c5bfd0186996f7713bd1463c9597c526abaad34c5

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.5-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 224.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.5-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 1361141f6d0ae928c99600db38c03d1ad14a1e26334256f6a01552a98816e3b1
MD5 e5e51990ba29aaa9f895d8e4d63379cd
BLAKE2b-256 c99abd25985b140f9138d62e5a3dc1fb1cada83975e3069fd062a7b7c7f6ea48

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pygjson-0.0.5-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 231.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.5-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b95e0e0b74fb8b3d014f4b21d8229f04c32902f019178149e2dbf5f9be08ae35
MD5 14d6181282011aa917b4bc4c34998a53
BLAKE2b-256 f45cd0a7825b32dfe925f317b5de3b2eb576dc7e7f74edca7f0ddd2f2550c281

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d897f2236646711eadc44d6bb72a8fbe290f2d3634434434f85199c80fc9c255
MD5 ef412e1cdd73522e3ac2bcb64f43c1b7
BLAKE2b-256 424646edc09e5c156e16c983f5d5924ef91bcaf3cc3da5031a877955329ffa43

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3042c7223e66703526b6a28b304ee083e63ed0793ff729aec1b4b7b9b5bfd784
MD5 2f29b402024382656f20c31b29191e25
BLAKE2b-256 2d3ed48c4280c155e61e37533ae766105f66984b0c6ac06b0e617688006b21b9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.5-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a5ccd20aa87cab74e3b6d2ce787abcae4b9a446d30afcf6012f880bc412cfe34
MD5 fc162c2a7690c73199613f93815d6649
BLAKE2b-256 6566304d19de89ed90dd56c830e8a8e0751209fdfbb6f57f2cd060db599648ae

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pygjson-0.0.5-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1accc98eca44926fb393a51181a10357f462e726440fc652c049dd65df03085e
MD5 18230bff8437dcafb674d8fb0fb45e1b
BLAKE2b-256 cbd39891f394270d8dd2bd332258493fb82216ff449887afca5b1be1524d92e0

See more details on using hashes here.

Provenance

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