Search code examples
jsonjolt

Given a JSON input file, that contains two arrays, How can I group every element of one array with the other array, using JOLT?


Given an input JSON file , that has two arrays:

[
  {
    "objects": ["obj1","obj2","obj3","obj4","obj5"]
  },
  {
    "types": ["kpi1","kpi2","kpi3","kpi4"]
  }
]

How can I transform it as the output is a JSON, that groups into individual objects every element from objects array , with all elements from types array.

The expected outcome should be:

{
 "new":[
   { "obj":"obj1","types": ["kpi1","kpi2","kpi3","kpi4"] },
   { "obj":"obj2","types": ["kpi1","kpi2","kpi3","kpi4"] },
   { "obj":"obj3","types": ["kpi1","kpi2","kpi3","kpi4"] },
   { "obj":"obj4","types": ["kpi1","kpi2","kpi3","kpi4"] },
   { "obj":"obj5","types": ["kpi1","kpi2","kpi3","kpi4"] }        
 ]
}

I have written a Jolt spec, but simply appends the types array in the new[] list. It does not update every object.

{
  "operation": "shift",
  "spec": {
    "*": {
      "objects": {
        "*": "new[].obj"
      },
      "types": {
        "@": "new[].&"
      }
    }
  }
}

The output is:

{
  "new" : [ {
    "obj" : "obj1"
  }, {
    "obj" : "obj2"
  }, {
    "obj" : "obj3"
  }, {
    "obj" : "obj4"
  }, {
    "obj" : "obj5"
  }, {
    "types" : [ "kpi1", "kpi2", "kpi3", "kpi4" ]
  } ]
}

Solution

  • What you need seems to looping within the indexes of the "objects" array after distinctively deriving the whole types array in order to be able to use within the second transformation such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*ects": {
              "*": { // indexes of the "objects" array
                "@": "new[&1].&(2,1)" // going 2 levels up the tree and grabbing first
                                      // replacement of asterisk by using &(2,1)
                                      // to replicate "obj"
              }
            },
            "types": "&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "new": {
            "*": { // indexes of the "new" array
              "@": "&2[&]", // &2 copies the literal "new" by traversing 2 levels up the tree
              "@2,types": "&2[&].types"
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here