Transforming JSON Object without root name





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying transform my JSON object with for-each but I don't have any root element. Here is my object, node numbers can be more.



[       
{
"id": "1",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "2",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "3",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
}
]


And what I'm trying with my xsl transform:



<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<jsonObject xmlns:json="http://json.org/">
<requestResponse>
<xsl:choose>
<xsl:when test="count(//id) > 0">
<returnCode>100</returnCode>
<returnMessage>SUCCESS</returnMessage>
</xsl:when>
<xsl:otherwise>
<returnCode>9999</returnCode>
<returnMessage>BUSINESS_FAULT</returnMessage>
</xsl:otherwise>
</xsl:choose>
</requestResponse>

<xsl:for-each select="@*|node()">
<xsl:if test="id">
<id>
<xsl:value-of select="/id" />
</id>
</xsl:if>
</xsl:for-each>
</jsonObject>
</xsl:template>
</xsl:stylesheet>


If I have root name of my object, I can handle it but I need some help here. Thanks in advance for any idea!










share|improve this question























  • Not sure if you want to do it automatic, but there are some tools for this like: oxygenxml.com/doc/versions/20.1/ug-editor/topics/… . Otherwise you will have to load the document into a variable and fn:tokenize() it. After that you would be able to loop through with an for-each and transform your json to an xml via some substrings.

    – Christian Mosz
    Nov 29 '18 at 6:58






  • 1





    Are you doing some kind of transformation of the JSON into XML before doing the XSLT transformation? XSLT 2.0 doesn't have any ability to handle JSON input, so I assume that's what you must be doing. It then depends entirely on how you are converting the JSON to XML: there are many libraries for this, and they all do it differently. If you move to XSLT 3.0 then you can control the JSON-to-XML conversion from within XSLT itself, or you can process the JSON natively without conversion to an XML node tree.

    – Michael Kay
    Nov 29 '18 at 8:08











  • @MichaelKay Yes, I didn't declare that but I'm getting JSON input, then I'm mapping(transform) to XML. I can transform If it exists one node. But this situation my service throws me JSON array. So, i can't get variables like //jsonObject/id for example.

    – Kerem Can
    Nov 29 '18 at 8:15











  • You need to show us what XML is being supplied to the transformation, because there are so many different ways of mapping JSON to XML.

    – Michael Kay
    Nov 29 '18 at 9:09


















1















I'm trying transform my JSON object with for-each but I don't have any root element. Here is my object, node numbers can be more.



[       
{
"id": "1",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "2",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "3",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
}
]


And what I'm trying with my xsl transform:



<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<jsonObject xmlns:json="http://json.org/">
<requestResponse>
<xsl:choose>
<xsl:when test="count(//id) > 0">
<returnCode>100</returnCode>
<returnMessage>SUCCESS</returnMessage>
</xsl:when>
<xsl:otherwise>
<returnCode>9999</returnCode>
<returnMessage>BUSINESS_FAULT</returnMessage>
</xsl:otherwise>
</xsl:choose>
</requestResponse>

<xsl:for-each select="@*|node()">
<xsl:if test="id">
<id>
<xsl:value-of select="/id" />
</id>
</xsl:if>
</xsl:for-each>
</jsonObject>
</xsl:template>
</xsl:stylesheet>


If I have root name of my object, I can handle it but I need some help here. Thanks in advance for any idea!










share|improve this question























  • Not sure if you want to do it automatic, but there are some tools for this like: oxygenxml.com/doc/versions/20.1/ug-editor/topics/… . Otherwise you will have to load the document into a variable and fn:tokenize() it. After that you would be able to loop through with an for-each and transform your json to an xml via some substrings.

    – Christian Mosz
    Nov 29 '18 at 6:58






  • 1





    Are you doing some kind of transformation of the JSON into XML before doing the XSLT transformation? XSLT 2.0 doesn't have any ability to handle JSON input, so I assume that's what you must be doing. It then depends entirely on how you are converting the JSON to XML: there are many libraries for this, and they all do it differently. If you move to XSLT 3.0 then you can control the JSON-to-XML conversion from within XSLT itself, or you can process the JSON natively without conversion to an XML node tree.

    – Michael Kay
    Nov 29 '18 at 8:08











  • @MichaelKay Yes, I didn't declare that but I'm getting JSON input, then I'm mapping(transform) to XML. I can transform If it exists one node. But this situation my service throws me JSON array. So, i can't get variables like //jsonObject/id for example.

    – Kerem Can
    Nov 29 '18 at 8:15











  • You need to show us what XML is being supplied to the transformation, because there are so many different ways of mapping JSON to XML.

    – Michael Kay
    Nov 29 '18 at 9:09














1












1








1








I'm trying transform my JSON object with for-each but I don't have any root element. Here is my object, node numbers can be more.



[       
{
"id": "1",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "2",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "3",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
}
]


And what I'm trying with my xsl transform:



<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<jsonObject xmlns:json="http://json.org/">
<requestResponse>
<xsl:choose>
<xsl:when test="count(//id) > 0">
<returnCode>100</returnCode>
<returnMessage>SUCCESS</returnMessage>
</xsl:when>
<xsl:otherwise>
<returnCode>9999</returnCode>
<returnMessage>BUSINESS_FAULT</returnMessage>
</xsl:otherwise>
</xsl:choose>
</requestResponse>

<xsl:for-each select="@*|node()">
<xsl:if test="id">
<id>
<xsl:value-of select="/id" />
</id>
</xsl:if>
</xsl:for-each>
</jsonObject>
</xsl:template>
</xsl:stylesheet>


If I have root name of my object, I can handle it but I need some help here. Thanks in advance for any idea!










share|improve this question














I'm trying transform my JSON object with for-each but I don't have any root element. Here is my object, node numbers can be more.



[       
{
"id": "1",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "2",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "3",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
}
]


And what I'm trying with my xsl transform:



<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<jsonObject xmlns:json="http://json.org/">
<requestResponse>
<xsl:choose>
<xsl:when test="count(//id) > 0">
<returnCode>100</returnCode>
<returnMessage>SUCCESS</returnMessage>
</xsl:when>
<xsl:otherwise>
<returnCode>9999</returnCode>
<returnMessage>BUSINESS_FAULT</returnMessage>
</xsl:otherwise>
</xsl:choose>
</requestResponse>

<xsl:for-each select="@*|node()">
<xsl:if test="id">
<id>
<xsl:value-of select="/id" />
</id>
</xsl:if>
</xsl:for-each>
</jsonObject>
</xsl:template>
</xsl:stylesheet>


If I have root name of my object, I can handle it but I need some help here. Thanks in advance for any idea!







json xml xslt wso2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 29 '18 at 5:15









Kerem CanKerem Can

175113




175113













  • Not sure if you want to do it automatic, but there are some tools for this like: oxygenxml.com/doc/versions/20.1/ug-editor/topics/… . Otherwise you will have to load the document into a variable and fn:tokenize() it. After that you would be able to loop through with an for-each and transform your json to an xml via some substrings.

    – Christian Mosz
    Nov 29 '18 at 6:58






  • 1





    Are you doing some kind of transformation of the JSON into XML before doing the XSLT transformation? XSLT 2.0 doesn't have any ability to handle JSON input, so I assume that's what you must be doing. It then depends entirely on how you are converting the JSON to XML: there are many libraries for this, and they all do it differently. If you move to XSLT 3.0 then you can control the JSON-to-XML conversion from within XSLT itself, or you can process the JSON natively without conversion to an XML node tree.

    – Michael Kay
    Nov 29 '18 at 8:08











  • @MichaelKay Yes, I didn't declare that but I'm getting JSON input, then I'm mapping(transform) to XML. I can transform If it exists one node. But this situation my service throws me JSON array. So, i can't get variables like //jsonObject/id for example.

    – Kerem Can
    Nov 29 '18 at 8:15











  • You need to show us what XML is being supplied to the transformation, because there are so many different ways of mapping JSON to XML.

    – Michael Kay
    Nov 29 '18 at 9:09



















  • Not sure if you want to do it automatic, but there are some tools for this like: oxygenxml.com/doc/versions/20.1/ug-editor/topics/… . Otherwise you will have to load the document into a variable and fn:tokenize() it. After that you would be able to loop through with an for-each and transform your json to an xml via some substrings.

    – Christian Mosz
    Nov 29 '18 at 6:58






  • 1





    Are you doing some kind of transformation of the JSON into XML before doing the XSLT transformation? XSLT 2.0 doesn't have any ability to handle JSON input, so I assume that's what you must be doing. It then depends entirely on how you are converting the JSON to XML: there are many libraries for this, and they all do it differently. If you move to XSLT 3.0 then you can control the JSON-to-XML conversion from within XSLT itself, or you can process the JSON natively without conversion to an XML node tree.

    – Michael Kay
    Nov 29 '18 at 8:08











  • @MichaelKay Yes, I didn't declare that but I'm getting JSON input, then I'm mapping(transform) to XML. I can transform If it exists one node. But this situation my service throws me JSON array. So, i can't get variables like //jsonObject/id for example.

    – Kerem Can
    Nov 29 '18 at 8:15











  • You need to show us what XML is being supplied to the transformation, because there are so many different ways of mapping JSON to XML.

    – Michael Kay
    Nov 29 '18 at 9:09

















Not sure if you want to do it automatic, but there are some tools for this like: oxygenxml.com/doc/versions/20.1/ug-editor/topics/… . Otherwise you will have to load the document into a variable and fn:tokenize() it. After that you would be able to loop through with an for-each and transform your json to an xml via some substrings.

– Christian Mosz
Nov 29 '18 at 6:58





Not sure if you want to do it automatic, but there are some tools for this like: oxygenxml.com/doc/versions/20.1/ug-editor/topics/… . Otherwise you will have to load the document into a variable and fn:tokenize() it. After that you would be able to loop through with an for-each and transform your json to an xml via some substrings.

– Christian Mosz
Nov 29 '18 at 6:58




1




1





Are you doing some kind of transformation of the JSON into XML before doing the XSLT transformation? XSLT 2.0 doesn't have any ability to handle JSON input, so I assume that's what you must be doing. It then depends entirely on how you are converting the JSON to XML: there are many libraries for this, and they all do it differently. If you move to XSLT 3.0 then you can control the JSON-to-XML conversion from within XSLT itself, or you can process the JSON natively without conversion to an XML node tree.

– Michael Kay
Nov 29 '18 at 8:08





Are you doing some kind of transformation of the JSON into XML before doing the XSLT transformation? XSLT 2.0 doesn't have any ability to handle JSON input, so I assume that's what you must be doing. It then depends entirely on how you are converting the JSON to XML: there are many libraries for this, and they all do it differently. If you move to XSLT 3.0 then you can control the JSON-to-XML conversion from within XSLT itself, or you can process the JSON natively without conversion to an XML node tree.

– Michael Kay
Nov 29 '18 at 8:08













@MichaelKay Yes, I didn't declare that but I'm getting JSON input, then I'm mapping(transform) to XML. I can transform If it exists one node. But this situation my service throws me JSON array. So, i can't get variables like //jsonObject/id for example.

– Kerem Can
Nov 29 '18 at 8:15





@MichaelKay Yes, I didn't declare that but I'm getting JSON input, then I'm mapping(transform) to XML. I can transform If it exists one node. But this situation my service throws me JSON array. So, i can't get variables like //jsonObject/id for example.

– Kerem Can
Nov 29 '18 at 8:15













You need to show us what XML is being supplied to the transformation, because there are so many different ways of mapping JSON to XML.

– Michael Kay
Nov 29 '18 at 9:09





You need to show us what XML is being supplied to the transformation, because there are so many different ways of mapping JSON to XML.

– Michael Kay
Nov 29 '18 at 9:09












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532292%2ftransforming-json-object-without-root-name%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532292%2ftransforming-json-object-without-root-name%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)