Skip to main content

jsom permutation library

Project description

JSON patch applier

A small, composable JSON transformation DSL implemented in Python.

The library lets you describe transformations as data (a list of steps) and then apply them to an input document. It supports JSON Pointer paths, custom JMESPath expressions, interpolation with ${...} syntax, and a set of built-in operations.

Features

  • JSON Pointer read/write with support for:
    • root pointers ('', '/', '.')
    • relative .. segments
    • list slices like /items[1:3]
  • String interpolation:
    • ${/path/to/node} — JSON Pointer lookup
    • ${int:/path} / ${float:/path} / ${bool:/path} — simple casters
    • ${? some.jmespath(expression) } — JMESPath with custom functions
  • Special values:
    • $ref — reference into the source document
    • $eval — nested DSL evaluation with optional $select
  • Rich set of operations:
    • set, copy, copyD, delete, assert
    • foreach, if, distinct
    • replace_root, exec, update
  • Schema helper: approximate JSON Schema generation for a given DSL script.

Basic usage

from json_patch_applier import apply_actions

source = {
    "users": [
        {"name": "Alice", "age": 17},
        {"name": "Bob", "age": 22}
    ]
}

actions = [
    # Start with empty list
    {"op": "replace_root", "value": []},

    # For each user - build a simplified object
    {
        "op": "foreach",
        "in": "/users",
        "as": "u",
        "do": [
            {
                "op": "set",
                "path": "/-",
                "value": {
                    "name": "${/u/name}",
                    "is_adult": {
                        "$eval": [
                            {"op": "replace_root", "value": False},
                            {
                                "op": "if",
                                "cond": "${?`${/u/age}` >= `18`}",
                                "then": [{"op": "replace_root", "value": True}]
                            }
                        ]
                    }
                }
            }
        ]
    }
]

result = apply_actions(actions, dest={}, source=source)
# result -> {"fullName": "Alice", "isAdult": True}

Schema generation

from json_patch_applier import build_schema

schema = build_schema(script)

schema is a JSON-Schema-like structure that you can use for documentation, validation or introspection.

Extending with custom operations

from json_patch_applier import register_op

@register_op("my_op")
def my_op(step, dest, src):
    # implement your logic here
    return dest

Any registered operation can then be used in DSL scripts via {"op": "my_op", ...}.

License

This package is provided as-is; feel free to adapt it to your project structure.

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

j_perm-0.1.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

j_perm-0.1.2-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file j_perm-0.1.2.tar.gz.

File metadata

  • Download URL: j_perm-0.1.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for j_perm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fa05cdc3e0bacbc4cd926d3aa29a79b0f8d80eb5cc875c55a3fee0038fcfec44
MD5 756c959e03f8f0229264d9106dd68b37
BLAKE2b-256 f4e3702740debd33a89ca8ed17db8393e7c5f24f0edafb15cbb4ce6a1c5ba845

See more details on using hashes here.

File details

Details for the file j_perm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: j_perm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for j_perm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77ebe3bed381f778841345ee45c06cecadf8a35c35cc67a35536e0a25e357d70
MD5 f9801cbbcc7a98bdec9ce39825478768
BLAKE2b-256 4f3bd1194998f5825acb01a0776c8756119bd55e291ed009d9f6141203d3661d

See more details on using hashes here.

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