xsl-list
[Top] [All Lists]

Re: [xsl] json to json transformation

2019-03-15 07:10:57
On Fri, Mar 15, 2019 at 4:31 PM Martin Honnen 
martin(_dot_)honnen(_at_)gmx(_dot_)de <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

In general I agree that conversion to XML allows easier processing of
and transformation of JSON but your simply example could also be done at
the XPath 3.1 level with map functions:

   <xsl:output method="json" indent="yes"/>

   <xsl:param name="json-string" as="xs:string">{
    "id" : 105,
    "medals" : [1, 2, 3],
    "fName" : "Mukul",
    "lName" : "Gandhi",
    "address" : {
       "street1" : "xyz",
       "street2" : "maddison avenue",
       "country" : "C1"
     }
}</xsl:param>

   <xsl:variable name="map1" select="parse-json($json-string)"/>

   <xsl:template match="/">
       <xsl:sequence select="map:remove(map:put($map1, 'name',
$map1?fName || ' ' || $map1?lName), ('fName', 'lName'))"/>
   </xsl:template>


 I tried above XSLT transformation with Saxon, and it produces following,

 {
  "name":"Mukul Gandhi",
  "address": {
    "street1":"xyz",
    "street2":"maddison avenue",
    "country":"C1"
   },
  "id":105,
  "medals": [
    1,
    2,
    3
   ]
 }

The order of keys in above JSON output is not same as the input JSON. In
the JSON, I wanted fName and lName pair to be replaced by the 'name' key,
and wished that order of keys in JSON was preserved.

I think, with XPath 3.1 maps the ordering of keys has no relevance, and
from that point of view your XPath 3.1 map solution above is fine. But if
order of keys in the JSON need to be retained, is something like JSON to
XML & back to JSON again the only way ? I guess, Mike's paper discusses
issues including the previous point in a general way.




-- 
Regards,
Mukul Gandhi
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>