likeGreen
likeGreen

Reputation: 1079

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

Upvotes: 2

Views: 2405

Answers (2)

mattyb
mattyb

Reputation: 12083

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

Upvotes: 2

Barbaros Özhan
Barbaros Özhan

Reputation: 65228

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.

Upvotes: 2

Related Questions