Home > Mobile >  how to partially replace a string in array in APACHE NIFI using jolt
how to partially replace a string in array in APACHE NIFI using jolt

Time:12-16

I want to always replace "abcd" with "abcd.india" and "xyxvv" with "ind.hello" present in website under information array, without modifying prefix and suffix

Input :

{
  "requestId": 1122344,
  "Name": "testing",
  "information": [
    {
      "website": "https://abcd/122/ty",
      "city": "pune",
      "pincode": false,
      "client_name": 5
    },
    {
      "website": "http://xyxvv/122/ty",
      "city": "delhi",
      "pincode": false,
      "client_name": 5
    }
  ],
  "ReasonText": "something",
  "Code": "ABCD"
}

Output :

{
  "requestId": 1122344,
  "Name": "testing",
  "information": [
    {
      "website": "http://abcd.india/122/ty",
      "city": "pune",
      "pincode": false,
      "client_name": 5
    },
    {
      "website": "http://ind.hello/122/ty",
      "city": "delhi",
      "pincode": false,
      "client_name": 5
    }
  ],
  "ReasonText": "something",
  "Code": "ABCD"
}

CodePudding user response:

You can consecutively use split, join and concat functions to split the string, and then conditionally convert substrings and concatenate back all the pieces such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "information": {
        "*": {
          "ht": "=split('://',@(1,website))",
          "ws": "=split('/',@(1,ht[1]))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "information": {
        "*": {
          "ws": {
            "0": {
              "abcd": {
                "#abcd\\.india": "&5[&4].ws"
              },
              "xyxvv": {
                "#ind\\.hello": "&5[&4].ws"
              }
            },
            "*": "&3[&2].ws"
          },
          "*": "&2[&1].&"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "information": {
        "*": {
          "ws": "=join('/',@(1,ws))",
          "website": "=concat(@(1,ht[0]),'://',@(1,ws))"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "information": {
        "*": {
          "ht": "",
          "ws": ""
        }
      }
    }
  }
]
  • Related