Search code examples
jsonapache-nifijolt

jolt transformation for the match


I want to use jolt transformation to check if data is matching or not.

Input:

{
  "nodes": [
    {
      "id": "Type:APPLICATION_COMPONENT, id:206180",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "WebFrontEnd",
      "viewPermission": true,
      "agentUp": false
    },
    {
      "id": "Type:APPLICATION_COMPONENT, id:206183",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "AccountManagement",
      "viewPermission": true,
      "agentUp": false
    },
    {
      "id": "Type:APPLICATION_COMPONENT, id:206188",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "PerLoanServices",
      "viewPermission": true,
      "agentUp": false
    },
    {
      "id": "Type:APPLICATION_COMPONENT, id:206182",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "PolicyServices",
      "viewPermission": true,
      "agentUp": false
    },
    {
      "id": "Type:APPLICATION_COMPONENT, id:206184",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "BizLoanServices",
      "viewPermission": true,
      "agentUp": false
    },
    {
      "id": "Type:APPLICATION_COMPONENT, id:206185",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "BalanceServices",
      "viewPermission": true,
      "agentUp": false
    },
    {
      "id": "Type:APPLICATION_COMPONENT, id:206186",
      "flowMapNodeType": "MAIN_DASHBOARD_NODE",
      "name": "SessionTracking",
      "viewPermission": true,
      "agentUp": false
    }
  ],
  "sourceNode": "Type:APPLICATION_COMPONENT, id:206183",
  "targetNode": "Type:APPLICATION_COMPONENT, id:206184"
}

If sourceNode is matching with node.id in nodes subarray then a new field will be added sourceNodeName which is the node.name field for the same id. Similarly targetNode is matching with node.id in node subarray then a new field will be added targetNodeName which is the node.name field for the same id.

Expected output for this input:

{
  "sourceNodeName" : "AccountManagement",
  "targetNodeName" : "BizLoanServices"
  "sourceNode" : "Type:APPLICATION_COMPONENT, id:206183",
  "targetNode" : "Type:APPLICATION_COMPONENT, id:206184"
}

Solution

  • You can use the following tranformation :

    [
      { // add keys to the objects, which are under "nodes" array, with "id" values  
        "operation": "shift",
        "spec": {
          "nodes": {
            "*": {
              "*": "@1,id.&"
            }
          },
          "*": "&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*Node": { // loop only within the "sourceNode" and "targetNode" attributes
            "*": {
              "@(2,&)": { "name": "&3&" } // &3 : Bring the key name after going 3 levels up
            },
            "@": "&" // key-value pair for the upper nodes, eg. "sourceNode" and "targetNode"
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here