Skip to content

Merge Transformation

Davi Paulino edited this page May 4, 2017 · 1 revision
Use: "@jdt.merge" : <Value> (Case sensitive)
Value Type: Behavior
Primitive Replaces the value of the current node with the given value
Object Recursively merges the object into the current node. Keys that are not present in the source file will be added.
If the object contains JDT attributes, apply them. See Attributes.
Array Applies merge with each element of the array as the transformation value.
In an explicit merge, if the transformation value should be the array, double brackets should be used (e.g. [[<value>]]). In a default transformation, this is not necessary.

Obs: If the transformation value does not match the source value for an already existing node, the transformation value will replace the existing one.

Path Attribute

The @jdt.path attribute can be used if a specific node or multiple nodes should be changed. It can also be used to change nodes within arrays. See Attributes for more information.

Source:

{
    "A": {
        "TransformThis": true
    },
    "B": {
        "TransformThis": false
    },
    "C": {
    },
    "D": {
        "TransformThis": "WrongValue"
    },
    "E": {
        "TransformThis": false,
        "Items": [
            {
                "Value": 10
            },
            {
                "Value": 20
            },
            {
                "Value": 30
            }
        ]
    }
}

Transform:

{
    //Executes for all nodes on this level
    "@jdt.merge" : [{
        "@jdt.path" : "$.*",
        "@jdt.value" : {
            "Default" : 0
        }
    },
    //This only executes for matching nodes
    {
        "@jdt.path" : "$[?(@.TransformThis == true)]",
        "@jdt.Value" : {
            "Transformed" : true
        }
    }],
    "E": {
        // Accessing objects in array
        "@jdt.merge" : {
            "@jdt.path" : "$.Items[?(@.Value < 15)]",
            "@jdt.value" : {
                "Value" : 15,
                "Changed" : true
            }
        }
    }
}

Result:

{
    "A": {
        "TransformThis" : true,
        "Default" : 0,
        "Transformed" : true
    },
    "B": {
        "TransformThis": false,
        "Default" : 0
    },
    "C": {
        "Default" : 0
    },
    "D": {
        "TransformThis": "WrongValue",
        "Default" : 0
    },
    "E": {
        "TransformThis": false,
        "Items": [
            {
                "Value" : 15,
                "Changed" : true
            },
            {
                "Value": 20
            },
            {
                "Value": 30
            }
        ],
        "Default" : 0
    }
}

Value Attribute

The @jdt.value attribute in a Merge is the only type that supports nested transformations. This means that transformations that should be executed in newly created or merged nodes can be added through this value.

Clone this wiki locally