B
B
bizir2015-10-18 12:43:34
Programming
bizir, 2015-10-18 12:43:34

How can I find out what JSON is being passed?

Engaged in the development of the server part for the application. I use Node js as a server. So, data from the application comes to the server in JSON format, it is parsed there, checked and transmitted back. But if you send any other string to the server
, for example "dhsdsds", then Node.js immediately crashes without giving any errors. Can someone tell me how to find out that the string is in JSON format?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
petyagrill, 2015-10-18
@bizir

var parsedData;

try {
    parsedData = JSON.parse(data)
} catch (e) {
    // is not a valid JSON string
}

Is this option not right for you?

E
Ernest Faizullin, 2015-10-18
@erniesto77

try {
    jQuery.parseJSON(response);
} catch(error) {
    // это не json
}

P
protven, 2015-10-18
@protven

The server on http twitches? Decent people use headers like
Content-Type: application/json . Filter by it.
Node.js, by the way, is suddenly not a "language for the server" and not a "language" at all.

S
Sergey, 2015-10-18
Protko @Fesor

as a language

you are using javascript as the language. Feel the difference.
it throws an exception somewhere inside when doing JSON.parse.
The Content-Type header, but there is a chance that the client might be writing a moron who will not correctly set this header, so you need to fix the error handling anyway. It's only in PHP that you can do nothing, and then only if you stick to the classic dying model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question