bazaarvoice / jolt

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

Array element remove based on the value compare #1085

Closed loveall closed 3 years ago

loveall commented 3 years ago

Is there any way we can remove elements from an array based on value which is there in different element ? In below example we want to remove array element if code is not listed in "codes" object.

Input to process is as below ,

{
  "configs": [
    {
      "code": "ABC",
      "configs": [
        {
          "code": "XYZ",
          "configs": [
            {
              "code": "PQR"
            }
          ]
        },
        {
          "code": "ABC",
          "configs": [
            {
              "code": "LMN"
            }
          ]
        }
      ]
    }
  ],
  "codes": {
    "ABC": "YES",
    "QWE": "YES"
  }
}

Expected Output :

{
  "configs": [
    {
      "code": "ABC",
      "configs": [
          {
          "code": "ABC"
        }
      ]
    }
  ]
}

Thank you so much for the help in advance.

lucioalmeida commented 3 years ago

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "configs": {
        "*": {
          "code": {
            "*": {
              "@2": "config.&[]"
            }
          }
        }
      },
      "codes": "codes"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "config": {
        "*": {
          "*": {
            "configs": {
              "*": {
                "code": {
                  "*": {
                    "@2": "config.&6[&5].config.&[]"
                  }
                }
              }
            },
            "*": "config.&2[&1].&0"
          }
        }
      },
      "codes": "codes"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "config": {
        "*": {
          "*": {
            "config": {
              "*": {
                "*": {
                  "configs": {
                    "*": {
                      "code": {
                        "*": {
                          "@2": "config.&9[&8].config.&6[&5].config.&[]"
                        }
                      }
                    }
                  },
                  "*": "config.&5[&4].config.&2[&1].&0"
                }
              }
            },
            "*": "config.&2[&1].&0"
          }
        }
      },
      "codes": "codes"
    }
  }, {
    "operation": "shift",
    "spec": {
      "config": "config",
      "codes": {
        "*": {
          "YES": {
            "$1": "codes[#3].key"
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "codes": {
        "*": {
          "key": {
            "*": {
              "@(4,config.&0)": {
                "*": {
                  "@5": "configs_filter.&2[&1].codes",
                  "*": "configs_filter.&2[&1].&0"
                }
              }
            }
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "configs_filter": {
        "*": {
          "*": {
            "codes": {
              "*": {
                "key": {
                  "*": {
                    "@(4,config.&0)": {
                      "*": {
                        "@5": "configs_filter.&8[&7].configs_filter.&2[&1].codes",
                        "*": "configs_filter.&8[&7].configs_filter.&2[&1].&0"
                      }
                    }
                  }
                }
              }
            },
            "config": null,
            "*": "configs_filter.&2[&1].&0"
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "configs_filter": {
        "*": {
          "*": {
            "configs_filter": {
              "*": {
                "*": {
                  "codes": {
                    "*": {
                      "key": {
                        "*": {
                          "@(4,config.&0)": {
                            "*": {
                              "*": "configs_filter.&11[&10].configs_filter.&8[&7].configs_filter.&2[&1].&0"
                            }
                          }
                        }
                      }
                    }
                  },
                  "config": null,
                  "*": "configs_filter.&5[&4].configs_filter.&2[&1].&0"
                }
              }
            },
            "*": "configs_filter.&2[&1].&0"
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "configs_filter": {
        "*": {
          "*": "configs[]"
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "configs": {
        "*": {
          "configs_filter": {
            "*": {
              "*": "configs[&3].configs[]"
            }
          },
          "*": "configs[&1].&0"
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "configs": {
        "*": {
          "configs": {
            "*": {
              "configs_filter": {
                "*": {
                  "*": "configs[&5].configs[&3].configs[]"
                }
              },
              "*": "configs[&3].configs[&1].&0"
            }
          },
          "*": "configs[&1].&0"
        }
      }
    }
  }]

output

{
  "configs" : [ {
    "configs" : [ {
      "code" : "ABC"
    } ],
    "code" : "ABC"
  } ]
}
loveall commented 3 years ago

wow , thank you so much for your time and effort lucioalmeida. really apricated your help.
let me study this script , but with first glance seems its good.