2

Dynamic Jolt spec to handle if subarray inside the JSON has single element, multiple element or subarray not exists

My jolt spec is working perfectly fine if we nested array inside the JSON has multiple elements, but if they send a JSON without the nested array or the nested array with 1 element its not working

Input 1

(Multiple elements in articleMainMaterial array) :

[
  {
    "payload": {
      "header": {
        "messageType": "ArticleSeason"
      },
      "articleSeason": {
        "groupModelNumber": "JTT47",
        "groupArticleNumber": "IT4876",
        "seasonCode": "20251",
        "salesLine": "551",
        "articleMainMaterial": [
          {
            "materialComposition": "95% CO/5% EL",
            "mainMaterialFlag": true
          },
          {
            "materialComposition": "99% CO/10% PP",
            "mainMaterialFlag": true
          }
        ]
      }
    }
  }
]

Input 2

(One element in articleMainMaterial array) :

[
  {
    "payload": {
      "header": {
        "messageType": "ArticleSeason"
      },
      "articleSeason": {
        "groupModelNumber": "JTT47",
        "groupArticleNumber": "IT4876",
        "seasonCode": "20251",
        "salesLine": "551",
        "articleMainMaterial": [
          {
            "materialComposition": "95% CO/5% EL",
            "mainMaterialFlag": true
          }
        ]
      }
    }
  }
]

Input 3

(No articleMainMaterial array) :

[
  {
    "payload": {
      "header": {
        "messageType": "ArticleSeason"
      },
      "articleSeason": {
        "groupModelNumber": "JTT47",
        "groupArticleNumber": "IT4876",
        "seasonCode": "20251",
        "salesLine": "551"
      }
    }
  }
]

Current Jolt Spec which I'm using :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "payload": {
          "articleSeason": {
            "groupArticleNumber": "groupArticleNumber",
            "seasonCode": "seasonCode",
            "groupModelNumber": "groupModelNumber",
            "salesLine": "salesLine",
            "articleMainMaterial": {
              "*": {
                "materialComposition": "materialComposition"
              }
            }
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "materialComposition": {
        "*": {
          "@(2,groupArticleNumber)": "[#2].groupArticleNumber",
          "@": "[#2].&2",
          "@(2,seasonCode)": "[#2].seasonCode",
          "@(2,groupModelNumber)": "[#2].groupModelNumber",
          "@(2,salesLine)": "[#2].salesLine"
        }
      }
    }
  }
]

Output Expected :

If there is one element one record should come. If there is no articleMainMaterial array, materialComposition should come as NULL

[
  {
    "groupArticleNumber": "IT4876",
    "materialComposition": "95% CO/5% EL",
    "seasonCode": "20251",
    "groupModelNumber": "JTT47",
    "salesLine": "551"
  },
  {
    "groupArticleNumber": "IT4876",
    "materialComposition": "99% CO/10% PP",
    "seasonCode": "20251",
    "groupModelNumber": "JTT47",
    "salesLine": "551"
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
  • *If there is one element one record should come. If there is no articleMainMaterial array, materialComposition should come as NULL* -> I've tried all, they are already so as you're desired, am I missing something ? – Barbaros Özhan Jun 20 '23 at 07:09

0 Answers0