0

I'm working in an application that sends to me a XML so I can work with the data in it. But now the application will send not only a XML, but also a JSON, depending on other variables.

Is there any way that I can verify if what has been sent is a JSON? Something equivalent to typeof or instanceof that will tell me that string is a JSON?

Edit: I'm giving maintenance in this application that has been built using a very, very bad programmed framework. Right now my "data" is the return of a function and I'm unable to get the Content-Type without refactoring good part of the framework - what would take me months - and I don't have this time right now.

Right now:

ajax.request('POST',function(data){
    xml = loadXML(data); // It's always a XML, so I simply load it.
    ...
    ..
})

What I need:

ajax.request('POST',function(data){
    if(valueCanBeJSON(data)){ // It's not always a XML. How can I do this verification?
        json = eval('('+data+')');
    }else{
        xml = loadXML(data);
        ...
        ..
    }
})
4
  • 1
    The server should specify the response type with the Content-Type header field. Commented May 12, 2011 at 14:31
  • well, if its xml the first character is going to be '<' Commented May 12, 2011 at 14:32
  • typeof would probably just say "string" or "text", since that's what XML and JSON are when they go across the wire and get stuffed into your response handler's variable. It's only after post-processing by jquery/javascript that they become JS native arrays/objects. Commented May 12, 2011 at 14:43
  • @Gumbo I'm giving maintenance in this application that has been built using a very, very bad programmed framework. Right now my "data" is the return of a function and I'm unable to get the Content-Type without refactoring good part of the framework - what would take me months - and I don't have this time right now. But you're right in a general context. Commented May 12, 2011 at 14:53

2 Answers 2

1

As @Gumbo commented, you can check the Content-Type HTTP response header field. Alternately, you could try to parse it — though don't use eval(). Use JSON.parse(). If you're using jQuery, $.parseJSON() or just $.ajax() (without specifying the data-type) will also work.

ajax.request('POST', function(data) {
    var isJSON;
    try {
        data = JSON.parse(data);
        isJSON = true;
    }
    catch (e) {
        isJSON = false;
    }

    if (isJSON) {
        // data is already parsed, so just use it
    }
    else {
        // try treating it as XML
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

If the server is working correctly, it should include a Content-type header that tells you the format of the response body. Also, if it's capable of sending the response as two different formats, then it should try to honor the Accept header in the request, and send the format that the client specifies.

I suggest you take a look at JQuery - it has facilities to handle this sort of thing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.