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 j_perm 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 j_perm 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 j_perm 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.3.tar.gz (12.2 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.3-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: j_perm-0.1.3.tar.gz
  • Upload date:
  • Size: 12.2 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.3.tar.gz
Algorithm Hash digest
SHA256 4234f87246fccfc234274eb5cb711f6cae55c698beca6a3b16cbe98fee1e1b23
MD5 69c90eadc47f5c5687f0a9e2f5e4ef9d
BLAKE2b-256 37e733ac6bd1686addfa8ba8cac502dfc0e1a4d9827d2e7d6fce95458e4db25f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: j_perm-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.7 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0b055b764556b6978976465cc52b9cd38ca69574346e926aeaf2be44deea4cdf
MD5 f749f8c97f3b998c4383cba0f7021388
BLAKE2b-256 8d0d451401d81bdd4b2a95bf3480aa52eaf339c7a7cd04052e343aa1b9adb8aa

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