在Ballerina中自动将XML转换为JSON

时间:2017-05-24 23:11:32

标签: ballerina

在WSO2 ESB中,更改一些属性可以将messageType从XML更改为JSON,最简单的方法是什么,甚至不关心格式,标签,这里只是一个例子:

string SfID = xmls:getString(soapResponse, "/*:queryResponse/*:result/*:records/*:Id/text()" );
string Sfusername = xmls:getString(soapResponse, "/*:queryResponse/*:result/*:records/*:Name/text()" );

json jsPayload = `{"SFUser":
                   {"Sfusername":"Sfusername" ,
                   "SfID":"SfID"}
                  }`;

jsons:set(jsPayload,"$.SFUser.SfID" , SfID);
jsons:set(jsPayload,"$.SFUser.Sfusername" , Sfusername);

message response = {};

messages:setJsonPayload(response,jsPayload);

我希望SOAPXMLResponse中的任何响应都能使用相同的标签转换为JSON。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

得到了同事的答案:

以下是示例(Salesforce执行):

@http:GET{}
    @http:Path {value:"/execute"}
    resource querySalesForceWithParams(message m ) {
        xml queryOptions = `<urn:QueryOptions xmlns:urn="urn:partner.soap.sforce.com"><urn:batchSize>200</urn:batchSize></urn:QueryOptions>`;
        headers = [queryOptions];
        soapResponse = salesforcesoap:ClientConnector.query(sales, headers, "Select c.Reason, c.IsClosed, c.Id, c.Description, c.CreatedDate From Case c");
        system:println(xmls:toString(soapResponse));

        message response = {};

        json jsonPayload = (json) soapResponse;

        messages:setJsonPayload(response,jsonPayload);    

        reply response;

    }