Jolt transform to put the json content in a field using NiFi

Viewed 370

My JSON Input

[
  {
    "id": "1234",
    "Status": "null",
    "Expired": "null",
    "First_Name": "null"
  },
  {
    "id": "5678",
    "Status": "null",
    "Expired": "null",
    "First_Name": "null"
  }
]

My JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].id"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "Status": "null",
        "Expired": "null",
        "First_Name": "null"
      }
    }
  }
]

Output:

[
  {
    "id": "1234",
    "First_Name": "null",
    "Expired": "null",
    "Status": "null"
  },
  {
    "id": "5678",
    "First_Name": "null",
    "Expired": "null",
    "Status": "null"
  }
]

Expected output:

{
  "isInput": true,
  "input": [
    {
      "id": "1234",
      "First_Name": "null",
      "Expired": "null",
      "Status": "null"
    },
    {
      "id": "5678",
      "First_Name": "null",
      "Expired": "null",
      "Status": "null"
    }
  ]
}

Can this be done using NiFi JoltTransformJSON. Like put the json content in a field "input" and add another isInput key. If not should I use replaceText with replacement value

{
  "isInput": true,
  "input": "${input}"
}

But, then how should I put output of JoltTransform to ${input} field

2 Answers

You could add the following two specs to your chain:

{
    "operation": "shift",
    "spec": {
      "isInput": "isInput",
      "*": "input[]"
    }
  },
  {
    "operation": "default",
    "spec": {
      "isInput": "true"
    }
  }

But it's shorter to just use this chain spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "input[&1].id"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "input[]": {
        "*": {
          "Status": "null",
          "Expired": "null",
          "First_Name": "null"
        }
      },
      "isInput": "true"
    }
  }

]

One step of shift step will suffice such as

[
  {
    "operation": "shift",
    "spec": {
      "#true": "isInput",
      "@": "input"
    }
  }
]

enter image description here

where @ sign key will copy all of the content for the current level.

Related