Azure APIM策略中移除非顶层的响应属性及值
Azure APIM 移除嵌套glossary属性解决方案
要移除嵌套在value数组元素的Properties中的glossary属性,官方顶层属性移除的方法不适用,直接用Liquid模板遍历嵌套结构就能解决,具体策略代码如下:
<set-body template="liquid"> { "value": [ {% for item in body.value %} { "Properties": { {% for prop in item.Properties %} {% if prop[0] != 'glossary' %} "{{ prop[0] }}": {{ prop[1] | json }}{% if forloop.last == false %},{% endif %} {% endif %} {% endfor %} } {% if forloop.last == false %},{% endif %} } {% endfor %} ] } </set-body>
代码说明
- 遍历
body.value数组里的每一个元素,逐个处理每个元素的Properties字段 - 再遍历
Properties里的所有键值对,跳过键名为glossary的属性,只保留其他属性及对应值 - 用
| json过滤器确保属性值的JSON格式正确,同时通过forloop.last处理逗号,避免出现语法错误
如果原始响应还有其他顶层字段(比如count、nextLink这类),只需在模板里补充对应字段即可,示例如下:
<set-body template="liquid"> { "count": {{ body.count }}, "nextLink": "{{ body.nextLink }}", "value": [ {% for item in body.value %} { "Properties": { {% for prop in item.Properties %} {% if prop[0] != 'glossary' %} "{{ prop[0] }}": {{ prop[1] | json }}{% if forloop.last == false %},{% endif %} {% endif %} {% endfor %} } {% if forloop.last == false %},{% endif %} } {% endfor %} ] } </set-body>
这个方案能批量处理数组内的所有元素,不管value数组有多少项,都会自动移除每个元素Properties里的glossary属性。
内容的提问来源于stack exchange,提问作者David Andrews
相关产品推荐
相关产品推荐

