Neeraj
Neeraj

Reputation: 49

conversion array into array by jolt

I have following json data . I want to transform following data by jolt nifi processor into result data

{
  "data": [
    {
      "source": "Environment Sensors",
      "alert_count": "2",
      "category": "envs",
      "alert_array": {
        "alerts": [
          {
            "name": "neeraj",
            "id": "123"
          },
          {
            "name": "arun",
            "id": "897"
          }
        ]
      }
    }
  ]
}

result data

{
  "alert": "2",
  "subcategory": "envs",
  "alert_array": [
    {
      "category": "Environment Sensors",
      "newName": "neeraj",
      "newID": "123"
    },
    {
      "category": "Environment Sensors",
      "newName": "arun",
      "newID": "897"
    }
  ]
}

Here value of category is the value of source

Upvotes: 0

Views: 87

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can use the following shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "alert_count": "alert",
          "category": "sub&",
          "alert_array": {
            "*": {
              "*": {
                "@(3,category)": "&3[&1].category",
                "name": "&3[&1].newName",
                "id": "&3[&1].newID"
              }
            }
          }
        }
      }
    }
  }
]

first reach the elements of the innermost object, and determine the value of the category element by going 3 levels up along with the others which are alreay located there.

Upvotes: 1

Related Questions