I have a input json in the format
[
{
"PERSON_ID": 78,
"EFFECTIVE_START_DATE": "2013-12-02 00:00:00",
"LAST_NAME": "Hulk78"
},
{
"PERSON_ID": 78,
"EFFECTIVE_START_DATE": "2020-06-24 07:29:26",
"LAST_NAME": "Hulks78"
},
{
"PERSON_ID": 79,
"EFFECTIVE_START_DATE": "2015-12-02 00:00:00",
"LAST_NAME": "Hulk79"
},
{
"PERSON_ID": 79,
"EFFECTIVE_START_DATE": "2020-07-24 07:29:26",
"LAST_NAME": "Hulks79"
},
{
"PERSON_ID": 80,
"EFFECTIVE_START_DATE": "2013-12-10 00:00:00",
"LAST_NAME": "Hulk15"
}
]
The expected output
[
{
"PersonId": 78,
"value": [
{
"EffectiveDate": "2013-12-02 00:00:00",
"lastName":"Hulk78"
},
{
"EffectiveDate": "2020-06-24 07:29:26",
"lastName":"Hulks78"
}
]
}
....
]
I want to transform the input json by grouping the person_id value and for each group add its respective effectiveDate and lastname in a array of values corresponding to that person id. Below is the xslt that i have tried.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="3.0"
xmlns="http://www.w3.org/2005/xpath-functions" xpath-default-namespace="http://www.w3.org/2005/xpath-functions" expand-text="yes">
<xsl:param name="input"/>
<xsl:output method="text"/>
<xsl:template name="xsl:initial-template">
<xsl:variable name="input-as-xml" select="json-to-xml($input)"/>
<xsl:variable name="transformed-xml" as="element(array)">
<array>
<xsl:for-each-group select="$input-as-xml" group-by="//number[@key='PERSON_ID']">
<map>
<string key="PersonId">
<xsl:value-of select="current-grouping-key()"/>
</string>
<array key="Value">
<xsl:for-each select="current-group()">
<map>
<string key="EffectiveDate">
<xsl:value-of select="../string[@key='EFFECTIVE_START_DATE']"/>
</string>
<string key="LASTNAME">
<xsl:value-of select="@LAST_NAME"/>
</string>
</map>
</xsl:for-each>
</array>
</map>
</xsl:for-each-group>
</array>
</xsl:variable>
<xsl:value-of select="xml-to-json($transformed-xml)"/>
</xsl:template>
</xsl:stylesheet>
Can anyone please help me in understanding how to take out each effective date and last name from the current group.
This is the output i am getting
[
{
"PersonId": "78",
"Value": [
{
"EffectiveDate": "",
"LASTNAME": ""
}
]
},
{
"PersonId": "79",
"Value": [
{
"EffectiveDate": "",
"LASTNAME": ""
}
]
},
{
"PersonId": "80",
"Value": [
{
"EffectiveDate": "",
"LASTNAME": ""
}
]
}
]