JOLT过滤器-根据两个不同JSON值的条件从嵌套数组中删除字段

ippsafx7  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(153)

我必须创建一个JOLT过滤器来删除基于条件的字段,而且该过滤器必须使用两种不同的JSON结构。

    • 所以,情况是这样的**

我有这些JSON:

    • 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": []
}

例如,我需要过滤器从attributes_here中删除key2key5(仅当service值为THIS_SERVICE时),并为JSON保留相同的结构。

    • 预期产出:**
    • 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": []
}

任何帮助都将不胜感激。谢谢你,祝你有一个伟大的一天!

piztneat

piztneat1#

可以使用以下等级库作为选项:

[
  {
    "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
      },
      "*": "&"
    }
  }
]
ia2d9nvy

ia2d9nvy2#

请尝试以下震动规格:

[
  {
    "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": {
        "*": "&"
      }
    }
  }
]

您可以根据需要在remove操作中删除字段。

相关问题