Home > Blockchain >  How to replace \n and \" from json value Apache Nifi?
How to replace \n and \" from json value Apache Nifi?

Time:05-18

Hello Nifi Folks and Professionals, The next record is a json flowfile response from Rest Api, i got it after an InvokeHTTP:

{
  "data": [
    {
      "description": "\n\n\"s, lorem Epsom jdfg\n\"",
      "offer": "new_one",
      "state": "save"
    },
    {
      "description": "\n\n\" a long text \n\"",
      "offer": "offer_two",
      "state": "save"
    }
  ],
  "info": {
    "per_page": 200,
    "count": 195,
    "page": 1,
    "more_records": false
  }
}

The next step was EvaluateJsonPath to get the list of records inside "data".

data = $.data

What i should do next is to clean the description text, by removing the new line characters \n and the "

I created the next regex expression to filter the non-needed chars but it doesn't work.

[\n|\"](?=[^,\"]*\"\s*:) 

I need the regex expression that remove the chars listed above from json record values.

CodePudding user response:

You can use JolttransformJSON processor with spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "*": {
          "dsc1": "=split('\\\"',@(1,description))",
          "dsc2": "=join('',@(1,dsc1))",
          "dsc3": "=split('\n',@(1,dsc2))",
          "description": "=join('',@(1,dsc3))"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "data": {
        "*": {
          "dsc*": ""
        }
      }
    }
  }
]

to split the values by those characters, and recombine the formed substrings in order to get rid of them

the demo on the site enter image description here

CodePudding user response:

If you're looking for a way to get this done strictly via regex, websites such as regex101.com and https://regex-generator.olafneumann.org/ are life savers. I would start there if you want to use regex.

  • Related