'convert json map to string in xslt

How to parse json map to string in xslt..

this my xslt template:-

<xsl:variable name="inputJosn" select="parse-json($input)"/>
<xsl:variable name="joined-array" as="map(*)*">
  <xsl:for-each-group select="$inputJosn?*" group-by="?fileName">
    <xsl:map>
      <xsl:map-entry key="'fileName'" select="current-grouping-key()"/>
      <xsl:map-entry key="'value'" select="map{'Root':array{map{'Lines': array:join(current-group()?value?*?Root?*?Lines)}}}"/>
    </xsl:map>
  </xsl:for-each-group>
</xsl:variable>

here joined-array variable is a map type.

so if pass a variable into parse-json($joined-array) and json-to-xml($joined-array) functions will throw an error..

An atomic value is required for the first argument of fn:parse-json(), but the supplied type is a map type, which cannot be atomized

so how to convert map type to string type.

 <xsl:variable name="json-array" select="parse-json($joined-array)"/>
 <xsl:variable name="json-xml" select="json-to-xml($joined-array)"/>


Solution 1:[1]

Given xsl:variable name="joined-array" as="map(*)*", the type is a sequence of maps, it is not quite clear what kind of conversion you want but you can serialize a sequence of maps with e.g. $joined-array ! serialize(., map { 'method' : 'json', 'indent' : true() }). That expression gives you a sequence of strings.

Solution 2:[2]

Your variable $joined-array contains a sequence of maps. If you converted this to JSON and then called parse-json() on the result you would end up with an array of maps.

There's a much more straightforward way of getting from a sequence of maps to an array of maps, namely array{$joined-array}.

However, if you want to convert your sequence of maps to a JSON string for some other reason, you can achieve this using the serialize() function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Honnen
Solution 2 Michael Kay