Reputation: 15750
Folks, Trying to understand returning and forming JSON responses.
The following code returns the object as a single string:
res.send(JSON.stringify(data));
Output to the browser:
{"Count":1,"Items":[{"dbsource":{"S":"x"},"number":{"S":"5002820"},"name":{"S":"blah,foo"},"expiration":{"S":"06/13/2015"},"type":{"S":"bar"}}]}
Dont I want the JSON output to be more readable, ie :
{
"one": "two",
"key": "value"
}
What should i change JSON.stringify(data) to? Ideally I want the response to be used as an API endpoint.
Thanks!
Upvotes: 0
Views: 284
Reputation: 10210
You are almost there. Use stringify with spaces
var str = JSON.stringify(data, undefined, 2);
The above string will have indentation with 2 spaces.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
Upvotes: 2