[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: json to json transformation

Subject: Re: json to json transformation
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 15 Mar 2019 11:01:25 -0000
Re:  json to json transformation
On 15.03.2019 11:42, Mukul Gandhi gandhi.mukul@xxxxxxxxx wrote:

Input JSON fileB person.txt:

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

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

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

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?

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.