0

I'm trying to fetch field inside json data

Input json

{
    "data": {
        "file": [{
                "id": "0001",
                "name": "harsha"
            },
            {
                "id": "0002",
                "name": "manohar"
            }
        ]
    }
}

XSLT style sheet

<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(map)">
            
            <xsl:for-each select="$input-as-xml/map/array[@key='file']">
                <map>
                    <string key="date">
                    <xsl:value-of select="../string[@key='name']"/>
                </string>
                </map>
            </xsl:for-each>
           
        </xsl:variable>
        <xsl:value-of select="xml-to-json($transformed-xml)"/>
    </xsl:template>
</xsl:stylesheet>

Here I'm trying to fetch name field but not able to fetch I'm getting empty data

{"date":""}

Any suggestions would be helpful...

2
  • Start by looking at what the XML produced by json-to-xml($input) looks like. Then adjust your processing accordingly. Commented Jun 7, 2021 at 9:00
  • I don't see how you would get a map at all, you use <xsl:for-each select="$input-as-xml/map/array[@key='file']"> but that doesn't select anything in your input. And which is the resulting XML and/or JSON you want to produce? You have two name values so building a map with two properties of the same name is not going to work. Commented Jun 7, 2021 at 10:58

2 Answers 2

1

The ".." looks wrong to me - why would you want the parent of the array element?

I would do this without conversion to/from XML. Something along the lines:

   <xsl:variable name="input-as-map" select="parse-json($input)" as="map(*)"/>
   <xsl:variable name="transformed-map" as="map(*)*">    
        <xsl:for-each select="$input-as-map?data?file?*">
             <xsl:map key="'date'">
                    <xsl:value-of select="?name"/>
             </xsl:map>
        </xsl:for-each>           
   </xsl:variable>
   <xsl:value-of select="serialize($transformed-map, map{'method':'json'}"/>

(untested, because I'm not sure what output you want).

Sign up to request clarification or add additional context in comments.

Comments

0

It is also not clear to me what you want, perhaps

   <xsl:for-each select="$input-as-xml/map/map[@key = 'data']/array[@key='file']">
        <map>
            <string key="date">{.//string[@key='name']}</string>
        </map>
    </xsl:for-each>

gives you a value you can work with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.