Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response JSON object or JSON.stringify?

Suppose I want to return JSON content

var content = {
  a: 'foo',
  b: 'bar'
};

What is the best practice to return my JSON data?

A) Return object as is; i.e res.end(content)?

B) JSON.stringify(content) and then call JSON.parse(content) on the client?

like image 565
dopplesoldner Avatar asked Mar 22 '14 17:03

dopplesoldner


People also ask

Is JSON response an object?

This is a response object that's returned by Google after a payer approves payment. Major API version. The value in the response matches the value provided in PaymentDataRequest .

When should I use JSON Stringify?

JSON. parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. JSON. stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a JSON string.

What is difference between JSON parse and JSON Stringify?

The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.

How do you Stringify a response object?

Stringify a JavaScript ObjectUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.


1 Answers

If you send the response with express's res.json you can send the Object directly as application/json encoded response.

app.get('/route/to/ressource', function(req, res){
  var oMyOBject = {any:'data'};

   res.json(oMyOBject);
});
like image 135
Bernhard Avatar answered Oct 02 '22 11:10

Bernhard