JOLT JSON Transformation single record to multiple record

Viewed 94

I have to do following transformation. In this a single record has to be split in 3 parts. cpu, user and nice. However need to have value of host and timestamp in all split as per they belong to particular cpu,user and nice

Input

[
  {
    "timestamp": "2021-10-29T18:13:31",
    "host": "bbtpnj33",
    "cpu": "1",
    "user": "5.34",
    "nice": "0"
  },
  {
    "timestamp": "2021-10-29T18:13:32",
    "host": "bbtpnj34",
    "cpu": "2",
    "user": "10",
    "nice": "5"
  }
]

Expected Output

[
  {
    "timestamp": "2021-10-29T18:13:31",
    "host": "bbtpnj33",
    "cpu": "1"
  },
  {
    "timestamp": "2021-10-29T18:13:31",
    "host": "bbtpnj33",
    "user": "5.34"
  },
  {
    "timestamp": "2021-10-29T18:13:31",
    "host": "bbtpnj33",
    "nice": "0"
  },
  {
    "timestamp": "2021-10-29T18:13:32",
    "host": "bbtpnj34",
    "cpu": "2"
  },
  {
    "timestamp": "2021-10-29T18:13:32",
    "host": "bbtpnj34",
    "user": "10"
  },
  {
    "timestamp": "2021-10-29T18:13:32",
    "host": "bbtpnj34",
    "nice": "5"
  }
]

I have to use NIFI JoltJsonTransformation for that need the JoltSpec. I did try many of them but not getting valid answer. I am new to Jolt, please help

My JOLT Expression

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "cpu": "[&1][#1].cpu",
        "user": "[&1][#1].user",
        "nice": "[&1][#1].nice"
      }
    }
  }
]
1 Answers

Use different qualifiers for those three attributes( &1_& ) and others( &1 ) as prefix in order to seperate them as two sets along with an auxiliary character such as an underscore( _ ) so as to use for distinguishing in the next shift operation. Btw, no need to repeat each key name as value, rather use an ampersand for each. And remove all the keys at the last step such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "cpu": "&1_&.&",
        "user": "&1_&.&",
        "nice": "&1_&.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*_*": {
        "@(1,&(0,1).timestamp)": "&1.timestamp",
        "@(1,&(0,1).host)": "&1.host",
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

enter image description here

Related