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,298
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

Not applicable

Hi @Dino, Can you helpme please?.

Thanks.

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>

hi kbouwmeester,

Thanks, i saw in last step from trace section and the problem is when executing XSL. the policie in JSON to XML is the payload is correct, but when executing XSL convert the attributs in other node.

The policie XSL add the namespace but not respec the attributes.

This is my XSLT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://schema.com.poc" version="1.0">
<xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
<!-- Stylesheet to inject namespaces into a document in specific places -->
<xsl:template match="/">
<soapenv:Envelope>
<soapenv:Header/>
<soapenv:Body>
<xsl:choose>
<!-- Handle 'Root' wrapper added by JSON to XML policy -->
<xsl:when test="normalize-space(/Root)">
<sch:pocSync>
<xsl:apply-templates select="node()|@*"/>
</sch:pocSync>
</xsl:when>
<!-- Handle 'Array' wrapper added by JSON to XML policy -->
<xsl:when test="normalize-space(/Array)">
<sch:pocSync>
<xsl:apply-templates select="node()|@*"/>
</sch:pocSync>
</xsl:when>
<!-- If the root element is not what was in the schema, add it -->
<xsl:when test="not(normalize-space(/pocSync))">
<sch:pocSync>
<xsl:apply-templates select="node()|@*"/>
</sch:pocSync>
</xsl:when>
<!-- everything checks out,  just copy the xml-->
<xsl:otherwise>
<xsl:apply-templates select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<xsl:template match="/Root/*" name="copy-root">
<xsl:element name="sch:{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>

<xsl:template match="/Array/*" name="copy-array">
<xsl:element name="sch:{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>

<xsl:template match="*[not(local-name()='Root') and not(local-name()='Array')]" name="copy-all">
<xsl:element name="{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<!-- template to copy the rest of the nodes -->
<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>