How to use "node-red-contrib-field-mapper" in Node-RED

Updated: Sep 24, 2021

Node-Red is a programming tool for wiring together hardware devices, APIs, and online services in new and interesting ways by using a visual builder.


What is node-red-contrib-field-mapper?


node-red-contrib-field-mapper module provides a “mapper” node in Node-RED for mapping JSON data from one format (i.e. input) to another (i.e. output).


This node can be helpful in scenarios where JSON data transformation is required or while integrating two different third-party APIs.


Let's understand this with an example.


Imagine you have an incoming JSON from a file, a node, or any API which contains user information (e.g. name, age, and address including street) and you would like to map address.street field to info.street in the JSON as shown below.



To accomplish this, we need a mapper with input and output JSON for transformation. The mapper JSON can be hosted anywhere either in your own environment, any public cloud, or within Krysp platform.


The URL endpoint of the hosted mapper JSON is required input to output transformation.

If you already have a mapper JSON hosted in your own environment or cloud with public access, Skip to "How to use node-red-contrib-field-mapper".


[OPTIONAL] To host the mapper JSON within Krysp Platform, follow the below instructions :


Step 1: Add HTTP in node from the palette and add the URL endpoint for mapper e.g. /mapper


Step 2: Add a change node and set msg.payload as JSON mentioned below with , and attach to HTTP in node



{
  "input" : {
      "name" : "",
      "age": "",
      "address" :{
          "street" : ""
      }
   },
   "output" :{
       "name" : "",
       "age": "",
       "info" :{
          "street" : ""
       }
    }
}


Step 3: Add an HTTP response node and add the Status code with 200 for a successful response and attach it to the previous change node.


Deploying a Mapper JSON using Krysp


Step 4: Click on Deploy, to publish the endpoint for the mapper JSON.


In this case, the endpoint will be http://<your-krysp-namespace>.krysp.cloud/mapper



How to use node-red-contrib-field-mapper?


Step 1: Install the node-red-contrib-field-mapper, following the documentation at https://flows.nodered.org/node/node-red-contrib-field-mapper.


Step 2: Add a node with input JSON that you need to transform e.g.



{
  "input" : {
      "name" : "Sherlock Homes",
      "age": "34",
      "address" :{
          "street" : "Baker Street"
      }
   }
}

Input JSON



Step 3: Add the mapper node from the palette to the workspace.


Step 4: Double click to open the Config node.


Step 5: Click on the edit icon to Add new mapping in the mapping field.


Step 6: Enter the URL endpoint for mapper JSON (as described in the previous section e.g. http://<your-krysp-namespace>.krysp.cloud/mapper) and Click Add.


Step 7: To map required fields from input to output data, double-click to open config node and click on add in Set Field Values. Select "info.street" (i.e. output JSON) to "address.street" (i.e. input JSON) to add the mapping.


Step 8: Attach a debug node to the mapper node to debug the transformed JSON.


On executing this flow, the expected output will be as follows,



{
  "output" : {
      "name" : "Sherlock Homes",
      "age": "34",
      "info" :{
          "street" : "Baker Street"
      }
   }
}

Output JSON


These types of transformations are often required while integrating or parsing data from one system to another e.g. fetching data from Salesforce and sending it to AWS.


Watch the demo here :



Krysp Platform enables low-code developers to seamlessly build applications/system integrations and deploy them to the cloud with minimal code or no code at all. Check out all the features offered by Krysp Platform and Quickstart templates to build apps faster.

65 views0 comments

Recent Posts

See All