How to tell if Body in HttpWebResponse is Json or XML

I am writing a a class in VB.Net which makes calls for RestAPI's via HttpWebRequest. All works fine and i get the response i am expecting.
What i am looking for is some simple method to evaluate the Body if it is a Json Object, Json Array or an XML doc so i know how to handle
the return data. I don't have any schema to validate against. I just want to know what type of data it is.
Confused CoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The HttpWebResponse object has a property called ContentType, you can analyze its content to guess the type of the request without looking the response body itself.
 ContentType content could be "text/xml" or "application/json" or similar, enough to make that decision.
Confused CoderAuthor Commented:
That would be great in an ideal world but its not 100% as I have for example an external API i am calling who returns
Content-Type: text/html; charset=UTF-8 even so the body is a Json doc. Also even should i go the route i still would not
know if its a single doc or an array of docs in my body.
Confused CoderAuthor Commented:
i patched together a basic function which does it for me now, it take the doc checks if it is a Json if we throw exception we check for xml and if we fail return invalid.

Private Function validate_doc(ByVal myBody As String) As String
        Dim DocType As String = ""
            DocType = JToken.Parse(myBody).Type.ToString
        Catch ex As Exception
                Dim xmlTest As String = XElement.Parse(myBody).FirstNode.ToString
                DocType = "XML"
            Catch ex2 As Exception
                DocType = "Ivalid"
            End Try
        End Try
        Return DocType
    End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Think, you are right, and analyzing the response content body is the only way to handle your problem.
BTW, instead of parsing the whole content, you just can try to search for some format specific signatures, like  "/>" for XML and "{" for json.
Also, parse is a quite heavy operation. Now you parse the content twice. First for detection in the validate_doc() function, then you throw the result away and then you need to parse again to extract data. If you still prefer parsing, I'd recommend to try to combine that to a one routine. Parse as JSON, if no exception, use it for further processing, same way the XML.
Confused CoderAuthor Commented:
Content type is not reliable to determine what is in body of response as well it does not tell me if its a Json Object or Array
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.