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"><?xml-multiple?></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.
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"><?xml-multiple?></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"
]
},