Home > database >  JOLT filter - Remove fields from nested array based on condition for two different JSON values
JOLT filter - Remove fields from nested array based on condition for two different JSON values

Time:01-21

I have to make a JOLT filter to delete a field based on condition, also the filter has to work with two different JSON structures.

So, here is the case.

I have these JSONs:

JSON 1:

{
  "header": {
    "key": "value"
  },
  "payload": {
    "someArray": [
      {
        "key1": "val1"
      }
    ],
    "elements": [
      {
        "service": "serviceOne",
        "something": "somethingValue"
      },
      {
        "service": "THIS_SERVICE",
        "attributes": [
          {
            "something": "somethingValue",
            "attributes_here": {
              "key1": "val1",
              "key2": "val2",
              "key3": "val3",
              "key4": "val4",
              "key5": "val5",
              "key6": "val6"
            }
          }
        ]
      }
    ]
  },
  "data": {
    "key1": "val1"
  }
}

JSON 2:

{
  "responseMessage": {
    "header": {
      "key": "value"
    },
    "payload": {
      "someArray": [
        {
          "key1": "val1"
        }
      ],
      "elements": [
        {
          "service": "serviceOne",
          "something": "somethingValue"
        },
        {
          "service": "THIS_SERVICE",
          "attributes": [
            {
              "something": "somethingValue",
              "attributes_here": {
                "key1": "val1",
                "key2": "val2",
                "key3": "val3",
                "key4": "val4",
                "key5": "val5",
                "key6": "val6"
              }
            }
          ]
        }
      ]
    },
    "data": {
      "key1": "val1"
    },
    "state": {
      "state1": "state1"
    }
  },
  "status": [],
  "notes": []
}

I need the filter to remove for example key2 and key5 from the attributes_here only if the service value is THIS_SERVICE and keep the same structure for the JSON.

Desired output:

JSON 1:

{
  "header": {
    "key": "value"
  },
  "payload": {
    "someArray": [
      {
        "key1": "val1"
      }
    ],
    "elements": [
      {
        "service": "serviceOne",
        "something": "somethingValue"
      },
      {
        "service": "THIS_SERVICE",
        "attributes": [
          {
            "something": "somethingValue",
            "attributes_here": {
              "key1": "val1",
              "key3": "val3",
              "key4": "val4",
              "key6": "val6"
            }
          }
        ]
      }
    ]
  },
  "data": {
    "key1": "val1"
  }
}

JSON 2:

{
  "responseMessage": {
    "header": {
      "key": "value"
    },
    "payload": {
      "someArray": [
        {
          "key1": "val1"
        }
      ],
      "elements": [
        {
          "service": "serviceOne",
          "something": "somethingValue"
        },
        {
          "service": "THIS_SERVICE",
          "attributes": [
            {
              "something": "somethingValue",
              "attributes_here": {
                "key1": "val1",
                "key3": "val3",
                "key4": "val4",
                "key6": "val6"
              }
            }
          ]
        }
      ]
    },
    "data": {
      "key1": "val1"
    },
    "state": {
      "state1": "state1"
    }
  },
  "status": [],
  "notes": []
}

Any help would be appreciated. Thank you and I wish you a great day!

CodePudding user response:

You can use the following spec as an option :

[
  {
    "operation": "shift",
    "spec": {
      "responseMessage": { // distinguish the elements whether they have an extra wrapper 
        "*": "&1_&"
      },
      "*": "&"
    }
  },
  { // accumulate the respective attributes by their "service" value within the "attributes_here" object
    "operation": "shift",
    "spec": {
      "*": "&",
      "*ayload": {
        "*": "&1.&",
        "elements": {
          "*": {
            "*": "&3.&2[#2].&",
            "attributes": {
              "*": {
                "*": "&5.&4[#4].&2[#2].&",
                "attributes_here": {
                  "*": "&6.&5[#5].&3[#3].&1.&",
                  "key2|key5": "&6.&5[#5].&3[#3].&1.@4,service.&"
                }
              }
            }
          }
        }
      }
    }
  },
  {// get rid of the keys with "THIS_SERVICE"
    "operation": "remove",
    "spec": {
      "*ayload": {
        "elements": {
          "*": {
            "attributes": {
              "*": {
                "attributes_here": {
                  "THIS_SERVICE": ""
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*_*": {
        "@": "&(1,1).&(1,2)" // the value of the first and second asterisks, after going one level up the tree, respectively
      },
      "*": "&"
    }
  }
]

CodePudding user response:

Please try the below jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "#false": "has_responseMessage",
      "header": "&",
      "payload": "&",
      "data": "&",
      "responseMessage": {
        "*": "&",
        "@(2,status)": "rest_&.status",
        "@(2,notes)": "rest_&.notes",
        "#true": "has_responseMessage"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "payload": {
        "*": "&1.&",
        "elements": {
          "*": { // 0, 1
            "*": "&3.&2[&1].&",
            "service": {
              "@": "&4.&3[&2].&1",
              "THIS_SERVICE": {
                "@(2,attributes)": "&5.&4[&3].attributes_temp",
                "#attributes_temp": "&5.&4[&3].attributes_key"
              },
              "#attributes": "&4.&3[&2].attributes_key"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "has_responseMessage": "=lastElement",
      "payload": {
        "elements": {
          "*": {
            "attributes_key": "=lastElement"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "payload": {
        "elements": {
          "*": {
            "attributes_temp": {
              "*": {
                "attributes_here": {
                  "key2": "",
                  "key5": ""
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "payload": {
        "*": "&1.&",
        "elements": {
          "*": { // 0, 1
            "service": "&3.&2[&1].&",
            "something": "&3.&2[&1].&",
            "attributes_key": {
              "attributes_temp": {
                "@(2,attributes_temp)": "&5.&4[&3].attributes"
              },
              "attributes": {
                "@(2,attributes)": "&5.&4[&3].attributes"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "has_responseMessage": {
        "true": {
          "@(3,header)": "responseMessage.header",
          "@(3,payload)": "responseMessage.payload",
          "@(3,data)": "responseMessage.data",
          "@(3,state)": "responseMessage.state"
        },
        "false": {
          "@(3,header)": "header",
          "@(3,payload)": "payload",
          "@(3,data)": "data"
        }
      },
      "rest_responseMessage": {
        "*": "&"
      }
    }
  }
]

You can remove your fields in the remove operation as you want.

  • Related