JSONPath-driven remapping engine that produces JSON Patch operations.
Project description
json-remap-engine
A Python package providing a JSONPath-driven remapping engine that produces JSON Patch operations.
This is the Python version of the JavaScript json-remap-engine module, implementing the same interface and functionality.
Features
- 100% JavaScript compatibility - Same JSONPath syntax as the JavaScript version
- Transform JSON documents using JSONPath-based rules
- Generate JSON Patch operations (RFC 6902)
- Support for remove, replace, and move operations
- Comma-separated array indices -
$.arr[1,3,5]syntax fully supported - Comprehensive diagnostics and error reporting
- Type-safe rule creation helpers
- Full test suite (20/20 tests passing)
Installation
uv add json-remap-engine
Or with pip:
pip install json-remap-engine
Quick Start
from json_remap_engine import (
create_remove_rule,
create_replace_rule,
run_transformer,
format_patch,
)
# Example document
document = {
"user": {
"name": "John Doe",
"email": "john@example.com",
"password": "secret123",
},
"items": ["a", "b", "c", "d", "e"]
}
# Define transformation rules
rules = [
# Remove sensitive data
create_remove_rule("$.user.password"),
# Replace email with a sanitized version
create_replace_rule("$.user.email", "****@example.com"),
# Remove multiple array items using comma syntax (same as JavaScript!)
create_remove_rule("$.items[1,3]"), # Removes "b" and "d"
]
# Run the transformer
result = run_transformer(document, rules)
# Check the result
print(f"Success: {result['ok']}")
print(f"Transformed document: {result['document']}")
print(f"Operations: {format_patch(result['operations'])}")
JSONPath Syntax
The Python version supports the exact same JSONPath syntax as the JavaScript version:
# Comma-separated array indices (just like JavaScript!)
create_remove_rule("$.arr[1,3,5]")
# Array slices
create_remove_rule("$.arr[1:3]")
# Wildcards
create_replace_rule("$.items[*].active", True)
# Filter expressions
create_remove_rule("$.users[?(@.age < 18)]")
# Nested paths
create_replace_rule("$.user.profile.settings.theme", "dark")
API
Main Functions
run_transformer(document, rules)- Apply transformation rules to a documentformat_patch(operations, pretty=True)- Format JSON Patch operations as JSON string
Rule Creation
create_remove_rule(matcher, **options)- Create a rule to remove matched valuescreate_replace_rule(matcher, value, **options)- Create a rule to replace matched valuescreate_move_rule(matcher, target, **options)- Create a rule to move matched values
Path Utilities
analysis_path_to_json_path(path)- Convert analysis path to JSONPathanalysis_path_to_pointer(path)- Convert analysis path to JSON Pointerpointer_to_analysis_path(pointer)- Convert JSON Pointer to analysis pathpointer_exists(document, pointer)- Check if a pointer exists in documentget_value_at_pointer_safe(document, pointer)- Safely get value at pointersimple_json_path_to_pointer(expression)- Convert simple JSONPath to pointer
Rule Options
All rule creation functions support these options:
id- Custom rule identifier (auto-generated if not provided)allow_empty_matcher- Allow matcher to resolve to zero paths without warningallow_empty_value- Allow value to be empty/undefined without raising warningsdisabled- Start the rule disabled (returned in diagnostics but not executed)
Replace rules also support:
value_mode-"auto"(default) or"literal"to control value interpretation
Move rules also support:
target_mode-"auto"(default),"pointer", or"jsonpath"to control target interpretation
Development
This package uses uv for dependency management.
# Install dependencies
uv sync --extra dev
# Run tests
uv run pytest tests/ -v
# Run example
uv run main.py
# Build package
uv build
License
MIT - See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_remap_engine-0.1.0.tar.gz.
File metadata
- Download URL: json_remap_engine-0.1.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed43b4f353714d456425f357ce8337fab7b36416101e10aab3c17f235890dbb4
|
|
| MD5 |
c23962ed470d4cb1ed0627033f55fe59
|
|
| BLAKE2b-256 |
0820a1ad6566e778bc6f43277588073fb71e2fb03f188b4b41ae197561f00a70
|
Provenance
The following attestation bundles were made for json_remap_engine-0.1.0.tar.gz:
Publisher:
publish.yml on earada/json-remap-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
json_remap_engine-0.1.0.tar.gz -
Subject digest:
ed43b4f353714d456425f357ce8337fab7b36416101e10aab3c17f235890dbb4 - Sigstore transparency entry: 600947234
- Sigstore integration time:
-
Permalink:
earada/json-remap-engine@c9d9a8b1696555f8e52a647ed1a56b9eddfd6a55 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/earada
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c9d9a8b1696555f8e52a647ed1a56b9eddfd6a55 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file json_remap_engine-0.1.0-py3-none-any.whl.
File metadata
- Download URL: json_remap_engine-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad3526303bb1f69f326c7b0c9cea37ddb63e1a499b945938d18bbc59d025df02
|
|
| MD5 |
9e3ebc61cf4b6948b31e62b2523d941a
|
|
| BLAKE2b-256 |
2726e2e24ca2aa9b42ba8aad2049a67d594b7fc07eab16fb8eaf1c4ac099696f
|
Provenance
The following attestation bundles were made for json_remap_engine-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on earada/json-remap-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
json_remap_engine-0.1.0-py3-none-any.whl -
Subject digest:
ad3526303bb1f69f326c7b0c9cea37ddb63e1a499b945938d18bbc59d025df02 - Sigstore transparency entry: 600947239
- Sigstore integration time:
-
Permalink:
earada/json-remap-engine@c9d9a8b1696555f8e52a647ed1a56b9eddfd6a55 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/earada
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c9d9a8b1696555f8e52a647ed1a56b9eddfd6a55 -
Trigger Event:
workflow_dispatch
-
Statement type: