Skip to Content
author's profile photo Former Member
Former Member


Hi Experts,

I have a requirement in SAP HANA XSJS file where I need to accept input from UI in XML format and convert into JSON format.

I have used below code to convert my XML format to JSON

$.import('sap.hana.xs.lm.core', 'xml2json');

var xml2json = $.sap.hana.xs.lm.core.xml2json.xml2json;

var parser = new $.util.SAXParser();

var xmlString = ('<root><UserId>USER1</UserId> <supplier>123</supplier><Company>ABC</Company></root>');


var jsonobject = {};

jsonobject = xml2json.parse(xmlString); //Convert xml to json

object = jsonobject.root; //removed root object

//Stringy to convert

var json = JSON.stringify(object, function(key,value){

if(key == "parent"){

return undefined;


if(key == "attrs"){

return undefined;


return value;


but output am getting from the above statements is


I was expecting output in the below format


could you help me out with the solution to convert XML to JSON

And one more thing is based on user input I need to check JSON/XML format is there any method in Javascript (SAP HANA XS) to check the input format.



Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Sep 08, 2016 at 06:16 AM

    Hello Anu,

    the XML to JSON converter you used (which is a specific implementation for Lifecycle management requirements) adds the values of the xml to a "$" property. Therefore JSON.stringify produces the string as you get it. This behavior cannot be adjusted by options for the converter. If you wanna use the result you have to adjust it afterwards, but this is ugly from my point of view.

    If your XML structure is fix as you described it, following snippet can be used to convert the XML to a JS object which can then be stringified (of course if there are changes in the structure it is easy to adjust the snippet for your needs).

    // convert XML to JS Object
    function xml2Object(xml){
      var parser = new $.util.SAXParser();
      var result = {};
      var lastPropertyName = '';
      parser.startElementHandler = function(name, attrs){
        lastPropertyName = name;
        if(name === 'root'){
        result[name] = '';
      parser.characterDataHandler = function(value){
          result[lastPropertyName] = value;
      return result;
    // test
    var result = xml2Object('<root><UserId>USER1</UserId><supplier>123</supplier><Company>ABC</Company></root>');

    Your XML example is converted to following result with the snippet:

    Regarding the validation I do not know any available "out of the box" functionality on XS which lets you validate any kind of XML structure. But with the SAXParser functionality it would also easy to write a validator for your requirement.



    xx01.JPG (11.0 kB)
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.