xsl-list
[Top] [All Lists]

Re: [xsl] json to json transformation

2019-03-15 06:01:50
On 15.03.2019 11:42, Mukul Gandhi gandhi(_dot_)mukul(_at_)gmail(_dot_)com wrote:

Input JSON file person.txt:

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

I wish to transform above JSON document, into another JSON document which should look like following,

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

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>

BTW: What did you get the medals for?
--~----------------------------------------------------------------
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>