SimpleOne
SimpleOne

Reputation: 98

How to transform a flat JSON to nested JSON using NiFi

Input JSON :

{

  "type": "mbrInfo",
  "csId": 123456789,
  "insTS": "14-07-201911:55",
  "seqId": 1234565,
  "title": "Mr",
  "fName": "Amit",
  "mName": "",
  "lName": "V",
  "suffix": "Engg",
  "lvlId": "P",
  "lvlType": "LAC",
  "acctStatus": "20",
  "enrlDT": "2016-08-29",
  "vrsnId": 1
}

Expected Output JSON:

{

  "type": "mbrInfo",
  "csId": 123456789,
  "insTS": "14-07-201911:55",
  "seqId": 1234565,
   "name" : [{
  "title": "Mr",
  "fName": "Amit",
  "mName": "",
  "lName": "V",
  "suffix": "Engg"}],
  "lvlId": "P",
  "lvlType": "LAC",
  "acctStatus": "20",
  "enrlDT": "2016-08-29",
  "vrsnId": 1
}.

Currently I am using JOLTtransformJSON processor with JOLT Spec as :

[
{
    "operation": "shift",
    "spec": {
      "name": {
        "$": "[#1]",
        "@.title": "[#1].title",
        "@.fName": "[#1].fName",
        "@.mName": "[#1].mName",
        "@.lName": "[#1].lName",
        "@.suffix": "[#1].suffix"
      }
    }
  }
]

But all I am getting is either NULL or the original JSON (with diff spec) as output. Thanks in advance.

Upvotes: 0

Views: 1913

Answers (1)

mattyb
mattyb

Reputation: 12083

Is the intent to put all the name fields into a 1-element array containing an object. This JOLT spec puts them into an object at the name field:

[
  {
    "operation": "shift",
    "spec": {
      "title": "name.title",
      "fName": "name.fName",
      "mName": "name.mName",
      "lName": "name.lName",
      "suffix": "name.suffix",
      "*": "&"
    }
  }
]

...and this spec puts them into a 1-element array at the name field:

[
  {
    "operation": "shift",
    "spec": {
      "title": "name[0].title",
      "fName": "name[0].fName",
      "mName": "name[0].mName",
      "lName": "name[0].lName",
      "suffix": "name[0].suffix",
      "*": "&"
    }
  }
]

I don't see any other place in the input to get an index into the array, so I just used 0.

Upvotes: 1

Related Questions