jsonpath2path - JSON Data Transformation Tool.
Project description
jsonpath2path - JSON Data Transformation Tool
Overview
jsonpath2path is a powerful JSON data transformation tool that allows you to manipulate JSON structures using a simple yet expressive syntax. Inspired by JSONPath expressions, this tool provides advanced capabilities for modifying JSON data through path-based operations.
Refer to the document for the principle: JSON Path to Path
Key Features
- Intuitive path-based syntax for JSON manipulation
- Support for both value replacement and structural mounting
- Built-in conversion functions for common transformations
- Flexible source-target mapping (
1:1,N:N,1:N,N:1) - List operations with automatic appending for out-of-bound indices
Core Concepts
JSON as a Tree Structure
JSON data is treated as a tree where:
- Nodes represent values (dict, list, str, number, bool, or null)
- Edges represent dictionary keys or list indices
- "Slots" are positions in the tree that can hold either edges or nodes
Operation Types
- Occupy (
->): Replace the value at the target path - Mount (
=>): Attach edges and nodes to the target path
Mapping Relationships
1:1: Direct mapping without transformationN:N: Index-to-index mapping between source and target lists1:N: Source node is duplicated to match multiple targetsN:1: Multiple sources are appended/mounted to a single target
Command Syntax
( @jsonpath / jsonpath / `list` ) [ | convert_func [ param ]... ]... ( -> / => ) jsonpath
Components:
-
Source Specification (choose one):
@jsonpath: JSONPath with node valuesjsonpath: Regular JSONPath`list`: Backtick-wrapped 2D list in format[[k1,v1],[k2,v2],...]
-
Conversion Pipeline (optional, repeatable):
| convert_func [param]...: Apply conversion functions with parameters
-
Operation Specification (choose one):
->: Occupy (replace value)=>: Mount (attach structure)
-
Target JSONPath: Destination path for the operation
Built-in Converters
See the document: JSON Converter Functions Documentation
Usage Notes:
- All converters operate on the current node set
- Parameters can be:
- Literal values (numbers, strings, booleans)
- JSONPath expressions (for targeting sub-nodes)
- Multiple converters can be chained with
|Here's the updated Examples section that includes both the command syntax and functional API examples:
Examples
Example Data
{
"character": {
"name": "WarriorX",
"race": "Human",
"class": "Warrior",
"level": 25,
"skills": [
{
"name": "Sword Slash",
"damage": 50,
"cooldown": 3
},
{
"name": "Shield Bash",
"damage": 30,
"cooldown": 5
}
],
"equipment": {
"weapon": "Great Sword",
"armor": "Heavy Plate Armor"
},
"attributes": {
"health": 500,
"mana": 100,
"strength": 80,
"defense": 70
}
}
}
Addition Operations
- Add new attributes
- Command syntax:
`[["agility", 50], ["lucky", 20]]` => $.character.attributes
- Functional API:
(transformer.source([["agility", 50], ["lucky", 20]]) .pick(pick_type=PickType.CREATE) .assign("$.character.attributes", AssignType.MOUNT) .to(data))
- Command syntax:
Deletion Operations
-
Remove skills with long cooldowns
- Command syntax:
$.character.skills[?(@.cooldown > 4)] ->
- Functional API:
(transformer.source(data) .pick("$.character.skills[?(@.cooldown > 4)]", PickType.PLUCK))
- Command syntax:
-
Clear equipment
- Command syntax:
$.character.equipment.* ->
- Functional API:
(transformer.source(data) .pick("$.character.equipment.*", PickType.PLUCK))
- Command syntax:
Modification Operations
-
Adjust skill cooldowns
- Command syntax:
$.character.skills[*].cooldown | v_map "lambda v: v+5" => $.character.skills[*]
- Functional API:
(transformer.source(data) .pick("$.character.skills[*].cooldown", PickType.PLUCK) .v_map(lambda v: v+5) .assign("$.character.skills[*]", AssignType.MOUNT) .to(data))
- Command syntax:
-
Change character properties
- Command syntax:
`[["race", "Elf"]]` -> $.character.race `[["strength", 70], ["mana", 200]]` => $.character.attributes
- Functional API:
(transformer.source([["race", "Elf"]]) .pick(pick_type=PickType.CREATE) .assign("$.character.race", AssignType.OCCUPY) .to(data)) (transformer.source([["strength", 70], ["mana", 200]]) .pick(pick_type=PickType.CREATE) .assign("$.character.attributes", AssignType.MOUNT) .to(data))
- Command syntax:
Data Movement Operations
-
Move skills to new document
- Command syntax:
$.character.skills => $
- Functional API:
(transformer.source(data) .pick("$.character.skills", PickType.PLUCK) .assign("$", AssignType.MOUNT) .to(to_data))
- Command syntax:
-
Copy attributes to new document
- Command syntax:
@$.character.attributes => $
- Functional API:
(transformer.source(data) .pick("$.character.attributes", PickType.COPY) .assign("$", AssignType.MOUNT) .to(to_data))
- Command syntax:
Key Features Demonstrated:
- Both command syntax and functional API styles
- Mounting (
=>/AssignType.MOUNT) vs Occupying (->/AssignType.OCCUPY) operations - Different pick types (
PLUCK,COPY,CREATE) - Value modification using converters (
v_add,v_set) - Data movement between documents
The functional API provides more explicit control over each step of the transformation process, while the command syntax offers a more concise way to express common operations. Both styles can be used interchangeably depending on your preference and use case requirements.
Getting Started
- Install the package (installation method TBD)
- Import the module in your code
- Use the
transform_jsonfunction with your JSON data and transformation commands
Contribution
Contributions are welcome! Please submit issues or pull requests through GitHub.
License
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
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 jsonpath2path-0.1.0.tar.gz.
File metadata
- Download URL: jsonpath2path-0.1.0.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bfcd623eb3eb9484d3eabb5771c8c96a34a6527f564b5e8aa30ab0ebf75fe1e
|
|
| MD5 |
17d89b22959bdf394bcbd2ed33f77218
|
|
| BLAKE2b-256 |
829fb2dd750dd1c562895c24164ddecc64a36a96e8b232613cd8b5854ab68843
|
File details
Details for the file jsonpath2path-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jsonpath2path-0.1.0-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c67eda9c49dae1419d875bcadbbe18ca9f3ad40ccd9d94b02d92ffd73d90eb9e
|
|
| MD5 |
2bb8adfcfaa5b6db8948cdf0a2741663
|
|
| BLAKE2b-256 |
becb708c79d8711aeb344b0ec284d66d6276f2ca5218eacbeb590271fdfe13ab
|