Gihoon Lee
Gihoon Lee

Reputation: 13

json - jolt : How to extract common value to key

I am trying to convert a JSON to different format using JOLT (using NiFi JoltTransformJson processor).

Input Json

[
  {
    "date": "202001010000",
    "name": "test1",
    "val": "1",
    "status": "0"
  },
  {
    "date": "202001010000",
    "name": "test2",
    "val": "2",
    "status": "0"
  },
  {
    "date": "202001010001",
    "name": "test1",
    "val": "3",
    "status": "0"
  },
  {
    "date": "202001010001",
    "name": "test2",
    "val": "4",
    "status": "0"
  }
]

and I want to Output like

{
  "202001010000" : [ {
    "name" : "test1",
    "val" : "1",
    "status" : "0"
  }, {
    "name" : "test2",
    "val" : "2",
    "status" : "0"
  }
  ],
  "202001010001" : [ {
    "name" : "test1",
    "val" : "3",
    "status" : "0"
  }, {
    "name" : "test2",
    "val" : "4",
    "status" : "0"
  }
  ]
}

I'm trying to convert JSON format using Jolt Transform but it can't.

Upvotes: 1

Views: 583

Answers (1)

Jagadesh
Jagadesh

Reputation: 2116

  1. Make the date node value as key for each object in the array.
  2. Remove the date node from the object.

Spec :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "date": {
          "@1": "@(2,date)"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "date": ""
        }
      }
    }
  }
]

Upvotes: 1

Related Questions