JSON Pointer for Python

raw JSON →
3.1.1 verified Tue May 12 auth: no python install: verified quickstart: verified

A Python library for identifying specific nodes in a JSON document, implementing RFC 6901. Current version: 3.1.1. Release cadence: Regular updates with active maintenance.

pip install jsonpointer
error ModuleNotFoundError: No module named 'jsonpointer'
cause The 'jsonpointer' package has not been installed in your Python environment.
fix
pip install jsonpointer
error jsonpointer.JsonPointerException: index X out of bounds in [...]
cause The JSON pointer attempts to access an array index that is outside the bounds of the array in the JSON document.
fix
Ensure the array index specified in the JSON pointer is within the valid range (0 to length-1) for the target array. You can inspect the JSON document or add logic to check array bounds before applying the pointer.
error jsonpointer.JsonPointerException: JSON pointer must be empty or begin with '/'
cause The provided JSON pointer string does not conform to RFC 6901, as it does not start with a '/' character (unless it's an empty string for the root document).
fix
Correct the JSON pointer string to begin with a '/' (e.g., change 'foo/bar' to '/foo/bar') or use an empty string '' for the root document.
error KeyError: 'some_key'
cause When using jsonpointer.resolve() or JsonPointer().get() without a 'default' argument, a KeyError is raised if a segment of the pointer path references a key that does not exist in a dictionary within the JSON document.
fix
Provide a 'default' argument to jsonpointer.resolve(doc, pointer, default=None) or JsonPointer(pointer).get(doc, default=None) to return a specified value instead of raising a KeyError when the path is not found. Alternatively, ensure the JSON document structure matches the expected pointer path or explicitly check for key existence.
breaking Ensure correct import path to access JsonPointer class
fix Use 'from jsonpointer import JsonPointer' to import the class.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.00s 17.8M
3.10 slim (glibc) - - 0.00s 18M
3.11 alpine (musl) - - 0.01s 19.6M
3.11 slim (glibc) - - 0.00s 20M
3.12 alpine (musl) - - 0.00s 11.5M
3.12 slim (glibc) - - 0.00s 12M
3.13 alpine (musl) - - 0.00s 11.1M
3.13 slim (glibc) - - 0.00s 12M
3.9 alpine (musl) - - 0.00s 17.3M
3.9 slim (glibc) - - 0.00s 18M

Demonstrates how to use JsonPointer to access a specific value in a JSON document.

import json
from jsonpointer import JsonPointer

# Sample JSON document
json_data = {
    "store": {
        "book": [
            {"category": "reference",
             "author": "Nigel Rees",
             "title": "Sayings of the Century",
             "price": 8.95
            },
            {"category": "fiction",
             "author": "Evelyn Waugh",
             "title": "Sword of Honour",
             "price": 12.99
            }
        ]
    }
}

# Create a JsonPointer instance
pointer = JsonPointer('/store/book/0/author')

# Resolve the pointer to get the value
author = pointer.resolve(json_data)
print(author)  # Output: Nigel Rees