JOLT transformation for nested array

Viewed 38

I had broken my mind with JOLT. I need to group by data[].key then transform array in each group to object.

I had source JSON

{
    "status": "OK",
    "data": [
        {
            "value": 1,
            "key": "John",
            "subKey": "1W"
        },
        {
            "value": 1,
            "key": "John",
            "subKey": "3W"
        },
        {
            "value": 2,
            "key": "John",
            "subKey": "2W"
        },
        {
            "value": 4,
            "key": "John",
            "subKey": "older"
        },
        {
            "value": 5,
            "key": "Tharanya",
            "subKey": "2W"
        },
        {
            "value": 5,
            "key": "Tharanya",
            "subKey": "1W"
        }
    ]
}

Desired result is:

{
    "status": "OK",
    "data": [
        {
            "user": "John",
            "1W": 1,
            "2W": 2,
            "3W": 1,
            "older": 4  
        },
        {
            "user": "Tharanya",
            "2W": 5,
            "1W": 5
        }
    ]
}

This transformation aggregates flat-list to map

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "@": "data.@(1,key)"
        }
      }
    }
  }
]

This one transform array to object

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": "@(1,subKey)"
      }
    }
  }
]

I need help to combine this transformation to chain. Thank you.

IMAGE: Group transformation

IMAGE: array to object

1 Answers

You can mix up those current methods within the first shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "key": "&2.@(1,key).user",
          "@value": "&2.@(1,key).@subKey"
        }
      }
    }
  },
  {
  // get rid of key names of the objects within the "data" array 
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": "&1"
      }
    }
  },
  {
  // get rid of repeating components of "user" attribute
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": "ONE"
        }
      }
    }
  }
]
Related