Support Questions

Find answers, ask questions, and share your expertise

JOLT to flatten nested JSON

avatar
Explorer

Hi Team,

I'm trying to flatten a nested JSON and it is working fine for few cases which has data but for some cases if it has [] empty then the record is not showing up in the result. Could you please help me in tackling this corner case.

My data:

{
  "data": {
    "getVarietyListing": {
      "edges": [
        {
          "node": {
            "id": "346595",
            "identifier": 503882,
            "description": "Estefania (PL)",
            "productLifecycleManagementAreas": []
          }
        },
        {
          "node": {
            "id": "351741",
            "identifier": 504382,
            "description": "Amsterdam 2 - ABK",
            "productLifecycleManagementAreas": [
              {
                "code": {
                  "code": "X00",
                  "description": "Prepare to retire"
                },
                "startDate": "2025-09-01",
                "futureStartDate": null,
                "futureCode": null,
                "area": {
                  "country": "EA"
                }
              },
              {
                "code": {
                  "code": "X00",
                  "description": "Prepare to retire"
                },
                "startDate": "2025-09-01",
                "futureStartDate": null,
                "futureCode": null,
                "area": {
                  "country": "FR"
                }
              },
              {
                "code": {
                  "code": "X00",
                  "description": "Prepare to retire"
                },
                "startDate": "2024-04-11",
                "futureStartDate": null,
                "futureCode": null,
                "area": {
                  "country": "IL"
                }
              },
              {
                "code": {
                  "code": "X00",
                  "description": "Prepare to retire"
                },
                "startDate": "2025-09-01",
                "futureStartDate": null,
                "futureCode": null,
                "area": {
                  "country": "NL"
                }
              },
              {
                "code": {
                  "code": "X00",
                  "description": "Prepare to retire"
                },
                "startDate": "2025-09-01",
                "futureStartDate": null,
                "futureCode": null,
                "area": {
                  "country": "TR"
                }
              },
              {
                "code": {
                  "code": "X00",
                  "description": "Prepare to retire"
                },
                "startDate": "2025-09-01",
                "futureStartDate": null,
                "futureCode": null,
                "area": {
                  "country": "GB"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

This is the data and I'm using the following JOLT:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "getVarietyListing": {
          "edges": {
            "*": {
              "node": {
                "productLifecycleManagementAreas": {
                  "*": {
                    "@(2,identifier)": "[&4].[&1].Variety",
                    "@(2,description)": "[&4].[&1].VarietyDescription",
                    "startDate": "[&4].[&1].StartDate",
                    "futureCode": {
                      "code": "[&5].[&2].FuturePlcCode"
                    },
                    "futureStartDate": "[&4].[&1].FutureStartDate",
                    "code": {
                      "code": "[&5].[&2].PlcCode",
                      "description": "[&5].[&2].PlcCodeDescription"
                    },
                    "area": {
                      "country": "[&5].[&2].Country"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
},


  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "[]"
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "Country": null
      }
    }
  }

]

For the identifier 503882, I'm not getting any result becuase of "productLifecycleManagementAreas": [] it is empty. I need to get identifier and also description and rest all attributes can be hardcoded to null.

Your help matters a lot.
Thank you

0 REPLIES 0