Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Attribute XML to JSON

Not applicable

Hi to all,

I have a proxy with SOAP service with the default configuration and i try send JSON payload but i have a problem when XML has a attribute.

This is my XML

<soapenv:Envelope xmlns:soapenv="http://api.poc.test"
 xmlns:sch="http://schema.poc.test">
   <soapenv:Header/> 
   <soapenv:Body> 
      <sch:ApiPocSync> 
         <MessageHeader> 
            <DateTime>2005-12-31T23:59:59.1234Z</DateTime> 
         </MessageHeader> 
         <Request> 
            <Detail> 
               <id>02001</id> 
               <ext>327489</ext> 
               <amount Code="MXN">20.0</amount> 
               <number>963</number> 
               <type>01</type> 
               <date>2016-07-07</date> 
            </PaymentDetail> 
         </Request> 
      </sch:ApiPocSync> 
   </soapenv:Body> 
</soapenv:Envelope>

JSON

"ApiPocSync": {
        "MessageHeader": { "DateTime": "2005-12-31T23:59:59.1234Z" },
        "Request": {
          "Detail": {
            "id": "02001",
            "ext": "327489",
            "amount": {
              "-Code": "MXN",
              "#text": "20.0"
            },
            "number": "963",
            "type": "01",
            "date": "2016-07-07"
          }
        }
      }

When i send this json in attribute XML is wrong

<amount>
 <_currencyCode>MNX</_currencyCode>
 <_text>20</_text>
 </amount> 

The correct is this

<amount Code="MXN">20.0</amount> 

this is the police xml to json

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="xml-to-json">
    <DisplayName>XML to JSON</DisplayName>
    <Options>
        <RecognizeNumber>true</RecognizeNumber>
        <RecognizeBoolean>true</RecognizeBoolean>
        <RecognizeNull>true</RecognizeNull>
        <NullValue>NULL</NullValue>
        <TextNodeName>TEXT</TextNodeName>
        <AttributePrefix>@</AttributePrefix>
        <AttributeBlockName>#attrs</AttributeBlockName>
    </Options>
    <OutputVariable>response</OutputVariable>
    <Source>response</Source>
</XMLToJSON>

I try add @in the attribute but is wrong.

Thanks.

Regards.

Solved Solved
0 3 5,529
1 ACCEPTED SOLUTION

Hello @Josh iQu,

I assume you want to transform JSON to XML?

In order to get this:

<amount Code="MXN">20.0</amount>

you need to submit this:

 "amount": {
     "#attrs": {
          "@Code": "MXN"}
      , "#text": "20.0"
  }

This is my JSON to XML policy:

<JSONToXML async="false" continueOnError="false" enabled="true" name="JSON-to-XML">
    <DisplayName>JSON to XML</DisplayName>
    <Properties/>
    <Options>
        <NullValue>NULL</NullValue>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
        <NamespaceSeparator>:</NamespaceSeparator>
        <TextNodeName>#text</TextNodeName>
        <AttributeBlockName>#attrs</AttributeBlockName>
        <AttributePrefix>@</AttributePrefix>
        <InvalidCharsReplacement></InvalidCharsReplacement>
        <ObjectRootElementName>Root</ObjectRootElementName>
        <ArrayRootElementName>Array</ArrayRootElementName>
        <ArrayItemElementName>Item</ArrayItemElementName>
    </Options>
    <OutputVariable>XMLresponse</OutputVariable>
    <Source>request.content</Source>
</JSONToXML>

View solution in original post

3 REPLIES 3