-2

I need to find my string is XML or JSON object or string, based on this response I will make the string looks pretty.

So for that, I need to identify what kind of object. Can anyone please help me to find a way to achieve this?

Thanks in advance.

1 Answers1

2

try like this

   function GetInputType(response) {
    try {
      //try to parse via json 
      a = JSON.parse(response);
      return 'json type';
    } catch(e) {
     //try xml parsing 
     let parser = new DOMParser;
     var xmlDoc = parser.parseFromString(response,"application/xml");
        if(xmlDoc.documentElement.nodeName == "parsererror")
           return 'string type';
        else 
           return 'xml type';
   }     
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263