python-json-pointer

repository·master·Indexed 41 days ago

https://github.com/stefankoegl/python-json-pointer

A Python library and CLI tool for resolving and modifying JSON pointers as defined in RFC 6901. It provides the resolve_pointer() and set_pointer() methods for navigating Python objects, a JsonPointer class for reusable paths, and a jsonpointer command-line utility to resolve pointer expressions against JSON files.

Tokens
2K
Snippets
7
Records
10
Agent score
99%

What's inside python-json-pointer

  1. Overview of python-json-pointer

    master

    python-json-pointer is a Python library designed for resolving JSON pointers according to the RFC 6901 specification. It is suitable for applications that need to navigate or manipulate JSON documents using pointer strings.

    Requirements:

    • Python 3.10 or newer
    • PyPy is also supported
  2. Use the jsonpointer command-line utility

    master

    The jsonpointer CLI utility allows you to resolve JSON pointer expressions against one or more JSON files. You can provide the JSON pointer via a file containing the pointer expression, and then specify the target JSON files to be processed.

    # Example: resolving a pointer from ptr.json against a.json and b.json
    $ jsonpointer ptr.json a.json b.json
  3. Resolve JSON pointers using the CLI

    master

    To resolve a pointer, create a file containing the pointer string (e.g., "/a") and pass it as the first argument, followed by the JSON files you wish to inspect.

    Example workflow:

    1. Create a.json: { "a": [1, 2, 3] }
    2. Create ptr.json: "/a"
    3. Run: jsonpointer ptr.json a.json
    4. Result: [1, 2, 3]
    # inspect JSON files
    $ cat a.json
    { "a": [1, 2, 3] }
    
    $ cat b.json
    { "a": {"b": [1, 3, 4]}, "b": 1 }
    
    # inspect JSON pointer
    $ cat ptr.json
    "/a"
    
    # resolve JSON pointer
    $ jsonpointer ptr.json a.json b.json
    [1, 2, 3]
    {"b": [1, 3, 4]}
  4. Modify objects using set_pointer()

    master

    The set_pointer method allows you to modify a portion of a Python object using JSON pointer notation.

    By default, set_pointer performs an in-place modification of the original object. If you want to preserve the original object and receive a modified copy instead, pass inplace=False to the function.

    from jsonpointer import set_pointer
    obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
    
    # In-place modification (default)
    set_pointer(obj, '/foo/anArray/0/prop', 55)
    
    # Non-destructive modification (returns a copy)
    new_obj = set_pointer(obj, '/foo/anArray/0/prop', 55, inplace=False)
  5. Reuse paths with the JsonPointer class

    master

    The JsonPointer class allows you to wrap a JSON pointer string into an object. This is useful when you want to apply the same path to multiple different objects. You can use the .resolve(obj) method on the instance to retrieve the value at that path from a given object.

    import jsonpointer
    
    pointer = jsonpointer.JsonPointer('/foo/1')
    
    obj1 = {'foo': ['a', 'b', 'c']}
    print(pointer.resolve(obj1))  # Output: 'b'
    
    obj2 = {'foo': {'0': 1, '1': 10, '2': 100}}
    print(pointer.resolve(obj2))  # Output: 10
  6. Resolve JSON Pointers with resolve_pointer()

    master

    Use the resolve_pointer method to perform a deep retrieval from a Python object using an RFC 6901 compliant JSON pointer string. It acts similarly to a deep get operation. If the pointer path does not exist, you can pass None as the third argument to return None instead of raising an error.

    from jsonpointer import resolve_pointer
    obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
    
    # Resolve to the root object
    resolve_pointer(obj, '') == obj
    
    # Resolve to a nested key
    resolve_pointer(obj, '/foo/another prop/baz') == obj['foo']['another prop']['baz']
    
    # Resolve to an array index
    resolve_pointer(obj, '/foo/anArray/0') == obj['foo']['anArray'][0]
    
    # Return None for non-existent paths if specified
    resolve_pointer(obj, '/some/path', None) == None
  7. Reference the jsonpointer CLI arguments and options

    master

    The jsonpointer command follows this usage pattern:

    usage: jsonpointer [-h] [--indent INDENT] [-v] POINTER FILE [FILE ...]

    Positional Arguments

    • POINTER: A file containing a JSON pointer expression.
    • FILE: One or more files for which the pointer should be resolved.

    Optional Arguments

    • -h, --help: Show this help message and exit.
    • --indent INDENT: Indent the output by n spaces.
    • -v, --version: Show the program's version number and exit.
    usage: jsonpointer [-h] [--indent INDENT] [-v] POINTER FILE [FILE ...]
    
    Resolve a JSON pointer on JSON files
    
    positional arguments:
      POINTER          File containing a JSON pointer expression
      FILE             Files for which the pointer should be resolved
    
    optional arguments:
      -h, --help       show this help message and exit
      --indent INDENT  Indent output by n spaces
      -v, --version    show program's version number and exit
  8. Use the jsonpointer CLI to resolve JSON pointers

    master

    The jsonpointer CLI tool allows you to resolve a JSON pointer expression against one or more JSON files. You can provide the pointer either as a direct command-line argument or by reading it from a file. The tool outputs the resolved JSON object to standard output.

    Usage Patterns:

    1. Direct Pointer: Pass the pointer string as a positional argument.
    2. Pointer from File: Use the -f or --pointer-file flag to specify a file containing the pointer expression.
    3. Multiple Files: You can provide one or more JSON files to process. The tool will attempt to resolve the pointer for each file provided.
    4. Formatting Output: Use the --indent flag to specify the number of spaces for JSON indentation in the output.
    # Resolve a pointer directly from an argument
    ./bin/jsonpointer '/foo/bar' data.json
    
    # Resolve a pointer from a file containing the expression
    ./bin/jsonpointer -f pointer.txt data.json
    
    # Resolve a pointer and indent the output by 4 spaces
    ./bin/jsonpointer '/users/0/name' data.json --indent 4
    
    # Resolve a pointer against multiple JSON files
    ./bin/jsonpointer '/version' file1.json file2.json file3.json