bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 330 forks source link

Remove nested array element #1086

Closed loveall closed 3 years ago

loveall commented 3 years ago

how can we remove array element if flagToDelete is either ABC_CODE8 / ABC_NA / ABC_CODE6 (Basically "flagToDelete" values are dynamic and input "configs" are nested array )

Wanted to know if there is any way without custom jolt operation.

Input is :

{
    "body": {
        "allConfigs": [
            {
                "configs": [
                    {
                        "configs": [
                            {
                                "configs": [
                                    {
                                        "id": "12345",
                                        "code": "NA",
                                        "configs": [],
                                        "flagToDelete": "ABC_NA"
                                    },
                                    {
                                        "id": "66667",
                                        "code": "CODE1",
                                        "configs": [
                                            {
                                                "id": "12345",
                                                "code": "CODE7",
                                                "configs": [],
                                                "flagToDelete": "ABC_CODE7"
                                            },
                                            {
                                                "id": "66667",
                                                "code": "CODE8",
                                                "configs": [],
                                                "flagToDelete": "ABC_CODE8"
                                            }
                                        ],
                                        "flagToDelete": "ABC_CODE1"
                                    }
                                ],
                                "id": "67890",
                                "flagToDelete": "ABC_"
                            },
                            {
                                "id": "01234",
                                "code": "CODE2",
                                "configs": [
                                    {
                                        "id": "11111",
                                        "code": "CODE3",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE3"
                                    },
                                    {
                                        "id": "22222",
                                        "code": "CODE4",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE4"
                                    },
                                    {
                                        "id": "33333",
                                        "code": "CODE5",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE5"
                                    },
                                    {
                                        "id": "44444",
                                        "code": "CODE6",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE6"
                                    }
                                ],
                                "flagToDelete": "ABC_CODE2"
                            }
                        ],
                        "id": "66666",
                        "flagToDelete": "ABC_"
                    }
                ],
                "id": "11223",
                "desc": "Some info"
            }
        ],
        "id": "22334",
        "desc": "Some more info"
    }
}

Expected out put is :

{
    "body": {
        "allConfigs": [
            {
                "configs": [
                    {
                        "configs": [
                            {
                                "configs": [
                                    {
                                        "id": "66667",
                                        "code": "CODE1",
                                        "configs": [
                                            {
                                                "id": "12345",
                                                "code": "CODE7",
                                                "configs": [],
                                                "flagToDelete": "ABC_CODE7"
                                            }
                                        ],
                                        "flagToDelete": "ABC_CODE1"
                                    }
                                ],
                                "id": "67890",
                                "flagToDelete": "ABC_"
                            },
                            {
                                "id": "01234",
                                "code": "CODE2",
                                "configs": [
                                    {
                                        "id": "11111",
                                        "code": "CODE3",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE3"
                                    },
                                    {
                                        "id": "22222",
                                        "code": "CODE4",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE4"
                                    },
                                    {
                                        "id": "33333",
                                        "code": "CODE5",
                                        "configs": [],
                                        "flagToDelete": "ABC_CODE5"
                                    }
                                ],
                                "flagToDelete": "ABC_CODE2"
                            }
                        ],
                        "id": "66666",
                        "flagToDelete": "ABC_"
                    }
                ],
                "id": "11223",
                "desc": "Some info"
            }
        ],
        "id": "22334",
        "desc": "Some more info"
    }
}

Any suggestions are very much appreciated.

lucioalmeida commented 3 years ago

Configs has more than 3 levels?

loveall commented 3 years ago

Yes, it can be. We don't have control from downstream response.

lucioalmeida commented 3 years ago

Try with this spec

[
  //first level
  {
    "operation": "shift",
    "spec": {
      "body": {
        "allConfigs": {
          "*": {
            "configs": {
              "*": {
                "flagToDelete": {
                  "ABC_CODE8|ABC_NA|ABC_CODE6": null,
                  "*": {
                    "@2": "body.allConfigs[&5].configs[&3]"
                  }
                }
              }
            }
          }
        },
        "*": "body.&0"
      }
    }
  },
  //second level
  {
    "operation": "shift",
    "spec": {
      "body": {
        "allConfigs": {
          "*": {
            "configs": {
              "*": {
                "configs": {
                  "*": {
                    "flagToDelete": {
                      "ABC_CODE8|ABC_NA|ABC_CODE6": null,
                      "*": {
                        "@2": "body.allConfigs[&7].configs[&5].configs[&3]"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "*": "body.&0"
      }
    }
  },
  //third level
  {
    "operation": "shift",
    "spec": {
      "body": {
        "allConfigs": {
          "*": {
            "configs": {
              "*": {
                "configs": {
                  "*": {
                    "configs": {
                      "*": {
                        "flagToDelete": {
                          "ABC_CODE8|ABC_NA|ABC_CODE6": null,
                          "*": {
                            "@2": "body.allConfigs[&9].configs[&7].configs[&5].configs[&3]"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "*": "body.&0"
      }
    }
  },
  //remove null values in array
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]
loveall commented 3 years ago

thanks again lucioalmeida.