JSON String to JSON Transformer transforms JSON strings into a JSON object.

Take a look at the configuration parameters of the component:

  • Model Path: JSON as string field path in dotted notation.

  • Preserve Original: if activated, the option preserves the original fields.

Messages flow

Input

The component waits for a message in any format, but will look for a path inside the JSON Field Path configuration property.

Output

The structure will be the same one of input, but with another JSON string property and its representation of the JSON object. In case of error, the "error" property will be created at the same level as the original property.

The JSON dotted notation will look for the root element being processed by the pipeline and traverse it according to the specifications given in the JSON Field Path property.

Example:

In a JSON Field Path representation containing a.b.c.d, "a" will be searched in the root element. Then it will be "b", then "c" and finally "d". If an array is found during the traversal, the algorithm will spawn a separate traversal track for each element in the array. The algorithm replaces all occurrences of the path as defined in JSON Field Path.

JSON to JSON String Transformer in Action

Config

{
"type": "transformer",
"stepName": "prepare-transformer",
"transformSpec": [
    {
    "operation": "default",
        "spec": {
            "payload": {
                "a": "a",
                    "b": [
                        {
                            "c": 2,
                            "d": 3
                        }
                    ]
                }
            }
        }
    ]
},
{
    "type": "json-to-json-string-transformer",
    "stepName": "json-to-json-string-transformer",
    "jsonFieldPath": "payload",
    "preserveOriginal": true
}

Result

{
"payload": "{\"a\":\"a\",\"b\":[{\"c\":2,\"d\":3}]}",
    "_payload": {
    "a": "a",
        "b": [
            {
                "c": 2,
                "d": 3
            }
        ]
    }
}
Did this answer your question?