删除 JSON 数组中出现的特定 JSONobject 的 WSO2 Mediator [英] WSO2 Mediator that removes specific JSONobject occurrences within a JSON array

查看:28
本文介绍了删除 JSON 数组中出现的特定 JSONobject 的 WSO2 Mediator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在 wso2 中使用 json 数组转换响应.我正在创建一个 wso2 流出调解器,用于删除 json 数组响应中的特定 json 对象

this is my first time transforming a response with json arrays in wso2. I am creating a wso2 outflow mediator that removes a specific json object within a json array response

使用payloadfactory序列中介,我试图从json数组中删除json对象price".我遵循了 wso2 文档,但它的以太我得到了一个空的主体响应,或者 wso2 拒绝了流出序列.

Using payloadfactory sequence mediator, I was trying to remove the json object "price" from the json arrays. I followed the wso2 documentation but its ether i get an empty body response or the wso2 rejects the outflow sequence.

示例响应:

{
  "results": [
    {
      "name": "user",
      "item": "test",
      "price": {
        "pricePerItem": 2.0,
        "currency": "USD"
      },
      "stat": {
        "groupId": 3,
        "groupName": "DELIVERED",
      }
    },
    {
      "name": "user2",
      "item": "test2",
      "price": {
        "pricePerItem": 4.0,
        "currency": "USD"
      },
      "stat": {
        "groupId": 4,
        "groupName": "DELIVERED",
      }
    }
    ]
}

我的流出代码:

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="test_outflow.xml" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
  <payloadFactory media-type="json">
        <format>
                $1
        </format>
        <args>
            <arg evaluator="json" expression="$.results"/>
        </args>
</payloadFactory>
 <script language="js"><![CDATA[
    var payload = mc.getPayloadJSON();
    results = payload.results;
    response = new Array();
           for (i = 0; i &lt; results.length; ++i) {
           delete payload[i].id;
           response[i] = l;
           }
           mc.setPayloadJSON(response);
           ]]></script>
 <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
</sequence>

预期结果:

{
  "results": [
    {
      "name": "user",
      "item": "test",
      "stat": {
        "groupId": 3,
        "groupName": "DELIVERED",
      }
    },
    {
      "name": "user2",
      "item": "test2",
      "stat": {
        "groupId": 4,
        "groupName": "DELIVERED",
      }
    }
    ]
}

你们有没有遇到过这种情况?谢谢!

If any of you have experience with this kind of situation? thanks!

推荐答案

您可以使用以下内联脚本来实现您的需求

You can use the following inline script to achieve your requirement

<script language="js" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[
    var payload = mc.getPayloadJSON();
    var results = payload.results;
    var response = new Array();
    for (var i = 0; i < results.length; ++i) {
        delete results[i].price;
        response[i] = results[i];
    }
    payload.results = response;
    mc.setPayloadJSON(payload);
]]></script>

<小时>

下面给出的是为以下模拟需求开发的示例外序列(突触)


Given below is a sample out-sequence (synapse) developed for the following mock requirement

实际响应(修改前):

{
  "results": [
    {
      "name": "Athiththan",
      "price": {
        "value": 120
      }
    },
    {
      "name": "athiththan11",
      "price": {
        "value": 100
      }
    }
  ]
}

异常响应(修改后):

{
  "results": [
    {
      "name": "Athiththan"
    },
    {
      "name": "athiththan11"
    }
  ]
}

外序列突触配置:

<?xml version="1.0" encoding="UTF-8"?>
<outSequence xmlns="http://ws.apache.org/ns/synapse">
    <script language="js"><![CDATA[
        var payload = mc.getPayloadJSON();
        var results = payload.results;
        var response = new Array();
        for (var i = 0; i < results.length; ++i) {
            delete results[i].price;
            response[i] = results[i];
        }
        payload.results = response;
        mc.setPayloadJSON(payload);
    ]]></script>
    <send/>
</outSequence>

这篇关于删除 JSON 数组中出现的特定 JSONobject 的 WSO2 Mediator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆