Search code examples
jsonapache-nifijolt

Jolt tranformation merge records


facing one issue with Jolt -

Current input -

[
  {
    "test1": "A",
    "test2": "X",
    "test3": "Y",
    "test4": "Z"
  },
  {
    "test1": "B",
    "test2": "X",
    "test3": "Y",
    "test4": "Z"
  }
]

Output required is

[
  {
    "test1": "A",
    "label": [
      {
        "test2": "X",
        "test3": "Y",
        "test4": "Z"
      }
    ]
  },
  {
    "test1": "B",
    "label": [
      {
        "test2": "X",
        "test3": "Y",
        "test4": "Z"
      }
    ]
  }
]

Basically to merge the three attributes under one and the rest one should be outside

Tried with below Jolt but not working -

[
  {
    "operation": "shift",
    "spec": {
      "test1": "&",
      "*": "row.&"
    }
  }
]

Solution

  • You can prepend the values of the attributes other than test1 by label[0] while distinguishing objects by prepending &1 as the leading in order to substitute the indexes of them element such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "test1": "&1.&",
            "*": "&1.label[0].&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here