@aurabx/jolt-js
v0.2.0
Published
JSON to JSON transformation library using the JOLT spec - TypeScript port of harmony-jolt
Readme
jolt-js
JSON to JSON transformation library using the JOLT spec.
TypeScript port of harmony-jolt, which itself is a port of the Java Jolt library.
Installation
npm install jolt-jsUsage
import { transform, TransformSpec } from 'jolt-js';
const input = {
id: 1,
name: "John Smith",
account: {
id: 1000,
type: "Checking"
}
};
const spec: TransformSpec = [
{
operation: "shift",
spec: {
name: "data.name",
account: "data.account"
}
}
];
const output = transform(input, spec);
// {
// data: {
// name: "John Smith",
// account: { id: 1000, type: "Checking" }
// }
// }Supported Operations
1. shift
Copy data from the input tree and put it in the output tree.
{
operation: "shift",
spec: {
"name": "data.name",
"*": "data.&" // Wildcard matching
}
}Wildcards
*- match everythingname1|name2|nameN- match any of the specified names
Special Characters
&- reference matched key from path (&(x,y)for specific level/match)$- reference key value@- reference data value at path[]- array index notation
2. default
Apply default values if not present in the input.
{
operation: "default",
spec: {
"phones": {
"code": "+1"
}
}
}3. remove
Remove data from the tree.
{
operation: "remove",
spec: {
"phones": {
"country": ""
}
}
}4. modify-overwrite-beta
Modify data using functions.
{
operation: "modify-overwrite-beta",
spec: {
"fullName": "=concat(@(1,firstName),' ',@(1,lastName))"
}
}Supported Functions
concat- Concatenate multiple stringstoLower- Convert string to lowercasetoUpper- Convert string to uppercasesubstring- Extract substring (start, end)trim- Remove leading/trailing whitespacejoin- Join array elements with separator
Modifier Variants
modify-overwrite-beta- Always writes the resultmodify-default-beta- Only writes if key doesn't existmodify-define-beta- Only writes if key doesn't exist or is null
License
Apache-2.0
