Search code examples
jsonapache-nifijolt

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


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


Solution

  • 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"
        }
      }
    ]