XMLToJSON returning "infinity"in response for large number

I am using XMLToJSON policy as below

Value is EntityUniqueID in XML -- > 14523E140256263(<EntityUniqueID>14523E140256263</EntityUniqueID>)

Value is EntityUniqueID in JSON after xmltoJson policy-- > "Infinity" ("EntityUniqueID": "Infinity")

Any idea why it is converting "14523E140256263" to "infinity" and now to resolve this ?

<?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>
    </Options>
    <OutputVariable>response</OutputVariable>
    <Source>response</Source>
</XMLToJSON>

 

Solved Solved
0 2 154
1 ACCEPTED SOLUTION

The format NeM in JavaScript, where N and M are numbers, is "exponential form". It denotes N times 10 raised to the power of M.

Therefore 3.125e7 is a way to express 31250000.

In your case, the M number is so large that the result is not representable as a number in JavaScript. I suspect that the ID in your original XML is not intended to be interpreted a number, but is instead just a string, that happens to have a single E within it. You can tell the XMLToJSON policy to not try to recognize that as a number, using the RecognizeNumber Option:

 

<XMLToJSON name="X2J-Whatever">
  <Options>
    <RecognizeNumber>false</RecognizeNumber>
  </Options>
  ...

 

This will apply to all fields in the XML that "look like" numbers, not just to that one ID field.

If this is unsatisfactory or inappropriate, then you will need to use a different conversion. The XMLToJSON policy is not configurable to recognize numbers in only some elements. As one alternative option, You could use an XSLT policy in Apigee, along with this stylesheet, to convert your document to JSON. (Yes, you can create an XSL stylesheet to emit JSON). In my tests, that conversion always treats strings formatted as NeM, as plain strings. If for some reason you want to treat 14523E140256263 in the XML as a string, but something more reasonable like 3.125e17 as a number, you'd need to add another condition into the XSL, to check for that. Specifically check the case of NeM, in which the M is a really large exponent, maybe anything more than 5 digits. Eg

 

  <xsl:template match="json:value" mode="json">
    <xsl:choose>
      <xsl:when test="node() and not(text())">
        <xsl:apply-templates mode="json"/>
      </xsl:when>
      <xsl:when test="text()">
        <xsl:choose>
          <!-- emit a double when the exponent is not too large -->
          <xsl:when test="(string(.) castable as xs:double) and matches(.,'^([.\d]{1,})[eE](\d{1,5})$')">
            <xsl:text/><xsl:value-of select="json:encode-string(.)"/><xsl:text/>
          </xsl:when>
          ...

 

View solution in original post

2 REPLIES 2

The format NeM in JavaScript, where N and M are numbers, is "exponential form". It denotes N times 10 raised to the power of M.

Therefore 3.125e7 is a way to express 31250000.

In your case, the M number is so large that the result is not representable as a number in JavaScript. I suspect that the ID in your original XML is not intended to be interpreted a number, but is instead just a string, that happens to have a single E within it. You can tell the XMLToJSON policy to not try to recognize that as a number, using the RecognizeNumber Option:

 

<XMLToJSON name="X2J-Whatever">
  <Options>
    <RecognizeNumber>false</RecognizeNumber>
  </Options>
  ...

 

This will apply to all fields in the XML that "look like" numbers, not just to that one ID field.

If this is unsatisfactory or inappropriate, then you will need to use a different conversion. The XMLToJSON policy is not configurable to recognize numbers in only some elements. As one alternative option, You could use an XSLT policy in Apigee, along with this stylesheet, to convert your document to JSON. (Yes, you can create an XSL stylesheet to emit JSON). In my tests, that conversion always treats strings formatted as NeM, as plain strings. If for some reason you want to treat 14523E140256263 in the XML as a string, but something more reasonable like 3.125e17 as a number, you'd need to add another condition into the XSL, to check for that. Specifically check the case of NeM, in which the M is a really large exponent, maybe anything more than 5 digits. Eg

 

  <xsl:template match="json:value" mode="json">
    <xsl:choose>
      <xsl:when test="node() and not(text())">
        <xsl:apply-templates mode="json"/>
      </xsl:when>
      <xsl:when test="text()">
        <xsl:choose>
          <!-- emit a double when the exponent is not too large -->
          <xsl:when test="(string(.) castable as xs:double) and matches(.,'^([.\d]{1,})[eE](\d{1,5})$')">
            <xsl:text/><xsl:value-of select="json:encode-string(.)"/><xsl:text/>
          </xsl:when>
          ...

 

I am using below XSLT to convert 'EntityUniqueID' to string but this facing same issue

Any idea why this is not working ?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:b="https://bridgerinsight.lexisnexis.com/BridgerInsight.Web.Services.Interfaces.10.0">
    
    <!-- Match all elements and attributes -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- Modify EntityUniqueID to string -->
    <xsl:template match="b:EntityUniqueID">
        <EntityUniqueID>
            <xsl:value-of select="string(.)"/>
        </EntityUniqueID>
    </xsl:template>
    
</xsl:stylesheet>