Search code examples
javajsontransformationjolt

Transform fields to list using jolt


I'am working on updating my jolt spec to generate the right output. I've done a first try but unfortunately there something missing in my spec in order to obtain a good result.

This is my json input

{
  "id": 111,
  "uuid": "b4915b22-d8c0-47bd-832c-18c0",
  "testKey1[xxx]": "identifier_1234",
  "testKey2[yyyy]": "identifier_14",
  "name": "name1",
  "testValue1[xxx]": "10",
  "testValue2[yyy]": "12",
  "address": "S2271",
  "updateTime": "2024-01-18T10:35:00Z"
}

And this is my jolt spec

[
  {
    "operation": "shift",
    "spec": {
      "uuid": "idBus",
      "id": "idTech",
      "name": "title",
      "address": "location",
      "updateTime": "lastUpdated",
      "testValue*": "myList&.value",
      "testKey*": "myList&.code"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "myList*": "myListOfFields[]"
    }
  }
]

Actual i have this output

{
  "idBus": "b4915b22-d8c0-47bd-832c-18c0",
  "idTech": 111,
  "title": "name1",
  "location": "S2271",
  "lastUpdated": "2024-01-18T10:35:00Z",
  "myListOfFields": [
    {
      "value": "identifier_1234"
    },
    {
      "value": "identifier_14"
    },
    {
      "code": "10"
    },
    {
      "code": "12"
    }
  ]
}

My desired output to be like

{
  "idBus": "b4915b22-d8c0-47bd-832c-18c0",
  "idTech": 111,
  "title": "name1",
  "location": "S2271",
  "lastUpdated": "2024-01-18T10:35:00Z",
  "myListOfFields": [
    {
      "code": "identifier_1234",
      "value": "10"
    },
    {
      "code": "identifier_14",
      "value": "12"
    }
  ]
}

Is there any missed piece in order to achieve the desired output ? Any help would be appreciate

Thanks.


Solution

  • You might handle the issue like this :

    [
      {
        "operation": "shift",
        "spec": {
          "uuid": "idBus",
          "id": "&Tech",
          "name": "title",
          "address": "location",
          "updateTime": "lastUpdated",
          "test*ue*\\[*": "mLOF&(0,2).value",
          "test*ey*\\[*": "mLOF&(0,2).code" // 2nd replacement from the current(0th) level 
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "mLOF*": "myListOfFields[]"
        }
      }
    ]
    

    where the trick is to separate by &(0,2) identifiers in order to get the ordinals 1, 2 within the testValue..[...] expressions.

    Edit :

    You could add the following spec to the end in order to convert the format of the values to Double :

      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "myListOfFields": {
            "*": {
              "value": "=toDouble"
            }
          }
        }
      }