Setting a dynamic target path in Mapper Snap

This example demonstrates how to set a dynamic target path in Mapper Snap.


dynamic-target-path-pipeline

Download this pipeline.

Here’s a scenario, wherein there's one mapping defined that takes a value from the input and builds a dynamic key for the output.
mapping-of-expression-with-target-path-dynamically
  • Input data: {"headers": {"num": 100}}

  • Expression: $headers.num—this reads the num field nested inside the headers object from the incoming document (based on your JSON Generator, this value is 100).

  • Target path: $.newval['10_'+'20_'+$headers.num+'_30']—this is a dynamic target path — instead of mapping to a fixed field name, it builds the field name itself

  • Output: [{"headers":{"num":100},"newval":{"10_20_100_30":100}}]—the output preview looks as shown below:
    target-path-output
Note: Because Pass Through is enabled on this Mapper, the original headers.num field is passed through along with the newly created newval object in the output. This expression dynamically generates a nested key name under newval by stitching together static text and the runtime value of num, then assigns the original num value to that dynamically named key.

How Target path field works?

The Target path field is designed to represent a static (or JSONPath-style) field location—not a general-purpose JavaScript expression. While it can accept dynamic bracket-notation like $.newval[someExpression], that someExpression portion is expected to resolve as a key reference (example, a field value or JSONPath), not an arbitrary string-concatenation expression chained with + operators.

So in this mapping:

Target path: $.newval['10_'+'20_' + $headers.num + '_30']

The '10_'+'20_' + $headers.num + '_30' part is not evaluated as a JavaScript concatenation the same way it would be in the Expression column. The Expression column fully supports JavaScript syntax (string concatenation and other functions) because it's designed for that purpose. However, the Target path column has limited expression support. It's primarily meant for path/key resolution, not multi-part string building.