Skip to content

Transformer ​

The Transformer block turns the data it receives into a new shape. You can pick and rename fields with a simple map, or switch to JavaScript for anything more involved.

When to use it ​

  • Rename fields to match what another service or your caller expects, like first_name to firstName.
  • Keep only the fields you want to send on, and drop the rest.
  • Do bigger reshaping in JavaScript: combine fields, turn a list into an object, change every item in a list.
  • Don't use it to add or remove items in a list. Use Array Operations.
  • Don't use it to make a decision. Use If Condition or Switch.

Inputs ​

FieldRequiredDefaultWhat it does
Field MapWhen Use JS is offemptyPairs of source field and new name. Each pair copies one field of the input to a field of the new object.
Use JSNooffOn: ignore the Field Map and build the result with a script instead.
JSWhen Use JS is onemptyThe script. The data is available as input, and whatever you return is the result.
Save output to variableNooffStore the result in outputs.<name>.

Outputs ​

HandleWhen it is used
Next (right)Always. The next block receives the new object.

Example ​

The input is:

json
{ "first_name": "Avery", "last_name": "Stone", "age": 31 }

With the Field Map:

Source fieldNew name
first_namefirstName
last_namelastName

Result (age was not in the map, so it is dropped):

json
{ "firstName": "Avery", "lastName": "Stone" }

With JS (turn Use JS on):

js
return { fullName: input.first_name + " " + input.last_name, isAdult: input.age >= 18 };

Result:

json
{ "fullName": "Avery Stone", "isAdult": true }

How it behaves ​

  • The Field Map builds a brand new object. Only the fields in the map are copied. Everything else is dropped.
  • Top-level fields only. The map reads input["first_name"]. It can't reach into address.city. Use JS for nested fields.
  • A missing field is left out. If the input doesn't have a mapped field, the new object simply won't have it. It is not an error.
  • The map needs an object. If the input is empty (null), the block fails and the Error Handler runs. To reshape a list, use JS, for example return input.map(x => ({ id: x.id }));.
  • Use JS wins. With Use JS on, the Field Map is ignored, but it is kept, so you can switch back.
  • Script errors fail the block. A script that throws an error triggers the Error Handler.
  • JS Runner: run any code, not only reshaping.
  • Array Operations: add, remove or filter items in a list.
  • Response: its Transform response option reshapes the body right before sending.
  • HTTP Request: reshape an API answer before you use it.

Released under the Apache License 2.0. Enterprise features are under the Fluxify Enterprise Edition License.