Home > other >  Jolt Spec - Flatten out Nested Array and copy root values
Jolt Spec - Flatten out Nested Array and copy root values

Time:12-11

I need to flat out a nested array and also copy the values from the outer node to each of the elements in the flattened array.

The productSizes nested array needs to be flatten into Data array and then the fields from the product node needs to be added to the Data array while maintaining the relationship.

Input JSON:

[
  {
    "product": {
      "productSizes": [
        {
          "productCode": "AA0714-001",
          "masterSizeCode": "1.5Y"
        },
        {
          "productCode": "AA0714-001",
          "masterSizeCode": "11C"
        }
      ],
      "genderAgeCode": "20",
      "divisionCode": "20"
    }
  },
  {
    "product": {
      "productSizes": [
        {
          "productCode": "BB0204-100",
          "masterSizeCode": "XL"
        },
        {
          "productCode": "BB0204-100",
          "masterSizeCode": "S"
        }
      ],
      "genderAgeCode": "01",
      "divisionCode": "30"
    }
  }
]

Expected Output JSON:

{
  "Data" : [ {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "1.5Y",
    "GC" : "20",
    "DC" : "20"
  }, {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "11C",
    "GC" : "20",
    "DC" : "20"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "XL",
    "GC" : "01",
    "DC" : "30"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "S",
    "GC" : "01",
    "DC" : "30"
  } ]
}

My Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product": {
          "productSizes": {
            "*": "Data[]"
          },
          "genderAgeCode": "Data[&2].GC",
          "divisionCode": "Data[&2].DC"
        }
      }
    }
  }
]

My Output

{
  "Data" : [ {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "1.5Y",
    "GC" : "20",
    "DC" : "20"
  }, {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "11C",
    "GC" : "01",
    "DC" : "30"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "XL"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "S"
  } ]
}

Not really sure how to establish the grouping in this case.

CodePudding user response:

You need to walk through the indexes of the productSizes array for all of the attributes in order to repeat as many elements as that array has. So, use this spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product": {
          "productSizes": {
            "*": {
              "@": "&[&4]",
              "@(2,genderAgeCode)": "&[&4].GC",
              "@(2,divisionCode)": "&[&4].DC"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "Data"
      }
    }
  }
]

where [&4] represents to traverse four times opening curly braces({) backwards in order to reach and grab the outermost index(the first * character within the spec part) and & to seperate the values due to the indexes (0 and 1) in order to distinguish them in &[&4] substitution

enter image description here

  • Related