1

I have a the following JSON response, that I'm getting after a XSLT transformation. The values in the "name" array needed to be present as "ABC","XYZ"

Payload

<Data>
 <Mapping>
   <LocationID>001</LocationID>
   <GeoX>1.00</GeoX>
   <GeoY>2.00</GeoY>
 </Mapping>
 <Mapping>
   <LocationID>002</LocationID>
   <GeoX>56.00</GeoX>
   <GeoY>42.00</GeoY>
 <Mapping>
</Data>

Current Code where the Destination object is implemented.

<xsl:template match="//Data">
   <Destination>
      <Locations>
          <xsl:text disable-output-escaping="yes">&lt;?xml-multiple?&gt;</xsl:text>
            <Name>
             <jsonArray>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'">"ABC"</xsl:when>
                     <xsl:when test="LocationID='002'">"XYZ"</xsl:when>
                     <xsl:otherwise>"NEW"</xsl:otherwise>
                  </xsl:choose>
                  <xsl:if test="position()!=last()">,</xsl:if>
               </xsl:for-each>
              </jsonArray>
            </Name>
        </Locations>
    </Destination>
</xsl:template>

XML Output

<Destination>
  <Locations>
    <Name>"ABC","XYZ"</Name>
  </Locations>
</Destination>

Problem XML-to-JSON Output

"Destination": [
    {
        "Locations": {
            "Name": [
                "\"ABC\",\"XYZ\""
            ]
        },

Expected JSON Output

"Destination": [
    {
        "Locations": {
            "Name": [
                "ABC","XYZ"
            ]
        },

This "\"ABC\",\"XYZ\"" escape characters happen when i'm converting the XML to JSON. Is there a way to overcome this.

Rooster
  • 157
  • 2
  • 9
  • Please post a [mcve] showing the input and a complete XSLT. Your current code does NOT produce the output you show. – michael.hor257k Jun 04 '20 at 21:24
  • Hi Micheal, I can't shared something similar, since i am unable to share the XSLT in whole. I understand if it's difficult, i'm using the previous payload, and i want to receive values inside the `Name` array as `"ABC","XYZ"`. i hope this makes things clear for you – Rooster Jun 04 '20 at 21:48
  • You are asking us to fix code that we cannot see. I am voting to close this question. – michael.hor257k Jun 04 '20 at 21:52
  • Sorry for the trouble, I understand. I will close it, i was thinking if we assign the ABC, and XYZ to a variable and concatenate at the end will that way will be able to achieve `"ABC","XYZ"` – Rooster Jun 04 '20 at 22:01
  • There is no problem in generating an output of `"ABC","XYZ" ` - as I already showed: https://xsltfiddle.liberty-development.net/ehW12fq. The problem is that you have additional code that escapes the quotes. We don't know how this code works, so there is no way to tell what input it needs to produce the expected JSON, if it's at all possible, or how it needs to be modified in case it's not. – michael.hor257k Jun 04 '20 at 22:30

1 Answers1

1

I was able to resolve this issue by changing the above code as following.

Previous Code:

<xsl:template match="//Data">
   <Destination>
      <Locations>
          <xsl:text disable-output-escaping="yes">&lt;?xml-multiple?&gt;</xsl:text>
            <Name>
             <jsonArray>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'">"ABC"</xsl:when>
                     <xsl:when test="LocationID='002'">"XYZ"</xsl:when>
                     <xsl:otherwise>"NEW"</xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
              </jsonArray>
            </Name>
        </Locations>
    </Destination>
</xsl:template>

Changed code:

<xsl:template match="//Data">
   <Destination>
      <Locations>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'"><Name>ABC</Name></xsl:when>
                     <xsl:when test="LocationID='002'"><Name>XYZ</Name></xsl:when>
                     <xsl:otherwise><Name>NEW</Name></xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
        </Locations>
    </Destination>
</xsl:template>

Output

"Destination": [
    {
        "Locations": {
            "Name": [
                "ABC",
                "XYZ"
            ]
        },
Rooster
  • 157
  • 2
  • 9