1
votes

BACKGROUND:

I want to alter my JSON slightly from

{
  "d0" : 0.0,
  "d1" : 0.0,
  "d2" : 0.0,
  "d3" : 0.0,
  "d4" : 0.0,
  "d5" : 0.0,
  "d6" : 0.0,
  "d7" : 0.0
}

to

{ "d0" : [0.0],
  "d1" : [0.0],
  "d2" : [0.0],
  "d3" : [0.0]
  "d4" : [0.0],
  "d5" : [0.0],
  "d6" : [0.0],
  "d7" : [0.0]

}

and I am following the reply to the question given here https://community.cloudera.com/t5/Support-Questions/Groovy-Script-in-ExecuteScript-Processor-To-Format-Date/td-p/230207

Accordingly, I use-

1.EvaluateJsonPath 2.UpdateAttribute 3.AttributesToJson

and that part of my Nifi processing looks like this

related nifi flow

My settings for the 3 processors are -

1.EvaluateJsonPath

settings/ configuration for EvaluateJsonPath

2.UpdateAttribute

settings/ configuration for UpdateAttribute

3.AttributesToJson

settings/ configuration for AttributesToJson

But the AttributesToJson just changes the order of the data in the flat JSON to be

{
  "d4" : [0.0],
  "d5" : [0.0],
  "d6" : [0.0],
  "d7" : [0.0],
  "d0" : [0.0],
  "d1" : [0.0],
  "d2" : [0.0],
  "d3" : [0.0]
}

QUESTION:

Why is the AttributesToJson behaving this way? How should I change it to the correct order starting d0 to d7? I need the correct order because this is supposed to be the body of a POST request.

1

1 Answers

0
votes

Use JoltTransformJson processor to get the json columns in the desired order and keep the following

spec:

[{
    "operation": "shift",
    "spec": {
      "d1": "d1",
      "d2": "d2",
      "d3": "d3",
      "d4": "d4",
      "d5": "d5",
      "d6": "d6",
      "d7": "d7"
    }
  }
]

Now output flowfile will be in the correct order you are expecting:

{
  "d1" : [ 0 ],
  "d2" : [ 0 ],
  "d3" : [ 0 ],
  "d4" : [ 0 ],
  "d5" : [ 0 ],
  "d6" : [ 0 ],
  "d7" : [ 0 ]
}

(Or)

Another way would be using Query Record processor configure Record Reader/writer and add new sql query with your column names in the correct order.