JOLT Transform multiple flat arrays into single nested array

28 Views Asked by At

I can't seem to figure out JOLT to the point of frustration..

I can bring all of the values into a single nested array with no issue, however I cannot add values to the root of the output.

Data:

[
  {
    "Q": "10",
    "W": "0.5",
    "O": "Oregon",
    "Status": "Valid",
    "Type": "SSD",
    "Number": "Samsung1234",
    "Total": "50",
    "Category": "Drive"
  },
  {
    "Q": "15",
    "W": "0.5",
    "O": "Seattle",
    "Status": "Valid",
    "Type": "SSD",
    "Number": "PNY1234",
    "Total": "100",
    "Category": "Drive"
  }
  ...
]

Current Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "data.drives"
    }
  }
]

Which gives me the following structure:

{
  "data" : {
    "drives" : [ {
      "Q" : "10",
      "W" : "0.5",
      "O" : "Oregon",
      "Status" : "Valid",
      "Type" : "SSD",
      "Number" : "Samsung1234",
      "Total" : "50",
      "Category" : "Drive"
    }, {
      "Q" : "15",
      "W" : "0.5",
      "O" : "Seattle",
      "Status" : "Valid",
      "Type" : "SSD",
      "Number" : "PNY1234",
      "Total" : "100",
      "Category" : "Drive"
    } ]
  }
 ...
}

What I need is:

{
  "data": {
    "Number": "Samsung1234",
    "Category": "Drive",
    "Type": "SSD",
    "drives": [
      {
        "Q": "10",
        "W": "0.5",
        "O": "Oregon",
        "Status": "Valid",
        "Total": "50"
      },
      {
        "Q": "15",
        "W": "0.5",
        "O": "Seattle",
        "Status": "Valid",
        "Total": "100"
      }
    ]
  }
 ...
}

Any help is greatly appreciated. Jolt remains mostly a mystery to me, I just can't wrap my head around it..

1

There are 1 best solutions below

2
Barbaros Özhan On

You can use the following transformation if the aim is to get the only the first Number, Category and Type values :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Number|Category|Type": "data.&",
        "*": "data.drives[&1].&" // all other attributes
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "drives": "MANY", // keep this array as it is
        "*": "ONE" // bring only the first components 
                   // of the arrays respectively
      }
    }
  }
]