Support Questions

Find answers, ask questions, and share your expertise

Jolt Transformation - nested array to root object

avatar
New Contributor

Having issues flattening the nested array in an array of objects back to its parent.

 

Given this JSON : 

 

[
  {
    "id": "13",
    "uom": "EA",
    "name": "pOne",
    "display_name": "displayOne",
    "brand": "brandOne",
    "categories": [
      {
        "level1": "cLevel1",
        "level2": "cLevel2"
      }
    ]
  },
  {
    "id": "87",
    "uom": "EA",
    "name": "pTwo",
    "display_name": "displayTwo",
    "brand": "brandTwo",
    "categories": [
      {
        "level1": "cLevel1",
        "level2": "cLevel2"
      }
    ]
  }
]

 

 

I want to transform to this:

 

[ {
  "pName" : "pOne",
  "pBrand" : "brandOne",
  "pVariant" : "displayOne",
  "pSku" : "13-EA-000",
  "categoryHierarchy" : "cLevel1/cLevel2"
}, {
  "pName" : "pTwo",
  "pBrand" : "brandTwo",
  "pVariant" : "displayTwo",
  "pSku" : "87-EA-000",
  "categoryHierarchy" : "cLevel1/cLevel2"
} ]

 

 

So far I have these specs:

 

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "productSku": "=concat(@(1,id),'-', @(1,uom),'-000')",
        "categories": {
          "*": {
            "category": "=concat(@(1,level1),'/', @(1,level2))"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].pName",
        "brand": "[&1].pBrand",
        "display_name": "[&1].pVariant",
        "productSku": "[&1].pSku",
        "categories": {
          "*": {
            "category": "[&1].categoryHierarchy"
          }
        }
      }
    }
 }
]

 


It is however putting ALL the category values into a single array in the first object in the list, rather than in each respective object:

 

[ {
  "pName" : "pOne",
  "pBrand" : "brandOne",
  "pVariant" : "displayOne",
  "pSku" : "13-EA-000",
  "categoryHierarchy" : [ "cLevel1/cLevel2", "cLevel1/cLevel2" ]
}, {
  "pName" : "pTwo",
  "pBrand" : "brandTwo",
  "pVariant" : "displayTwo",
  "pSku" : "87-EA-000"
} ]

 

 

What Am I missing?

 

Thank you 🙂

1 REPLY 1

avatar
New Contributor

 

You can try below spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "productSku": "=concat(@(1,id),'-', @(1,uom),'-000')",
        "categories": {
          "*": {
            "category": "=concat(@(1,level1),'/', @(1,level2))"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].pName",
        "brand": "[&1].pBrand",
        "display_name": "[&1].pVariant",
        "productSku": "[&1].pSku",
        "categories": {
          "*": {
            "category": "[#4].categoryHierarchy"
          }
        }
      }
    }
 }
 ]