Search code examples
jsonapache-nifi

Transform/round-off decimal numbers in Nifi


I am trying to POST a JSON file (using Apache NiFi) onto an app that only accepts values with max 10 decimal places.

JSON format:

{
  "timestamp" : "2016-04-17",
  "zoom" : 13,
  "dc" : 100,
  "CloudCoverPercentage" : 74.707,
  "mean" : 0.18735192480231655,
  "num" : 127,
  "FirstQuartile" : 0.142807444773004,
  "median" : 0.17882054533925174,
  "max" : 0.32004310344827586,
  "min" : 0.059890294413970674,
  "ThirdQuartile" : 0.22603810187853218,
  "StandardDeviation" : 0.06846369990524923
}

Question: How do I transform each decimal value in Apache Nifi, so it would only have 10 decimal places? I read somewhere that JoltTransformJSON can be used for this. How do I write the Jolt Spec for this kind of operation?


Solution

  • You can use UpdateRecord with a jsonReader(Infer Schema), JsonSetWriter (Inherit Schema)

    Input:

    {
      "timestamp" : "2016-04-17",
      "zoom" : 13,
      "dc" : 100,
      "CloudCoverPercentage" : 74.707,
      "mean" : 0.18735192480231655,
      "num" : 127,
      "FirstQuartile" : 0.142807444773004,
      "median" : 0.17882054533925174,
      "max" : 0.32004310344827586,
      "min" : 0.059890294413970674,
      "ThirdQuartile" : 0.22603810187853218,
      "StandardDeviation" : 0.06846369990524923
    }
    

    Output:

    [ {
      "timestamp" : "2016-04-17",
      "zoom" : 13,
      "dc" : 100,
      "CloudCoverPercentage" : 74.707,
      "mean" : 0.18735192480231655,
      "num" : 127,
      "FirstQuartile" : 0.142807444773004,
      "median" : 0.1788205453,
      "max" : 0.32004310344827586,
      "min" : 0.059890294413970674,
      "ThirdQuartile" : 0.22603810187853218,
      "StandardDeviation" : 0.06846369990524923
    } ]
    

    enter image description here