This may not be what you want, but if you just want it to look better, I would recommend:
console.log(JSON.stringify(products, null, 2));
which would give you
[
{
"name": "Sample Product Name"
},
{
"name": "Sample Product Name 2"
}
]
In the console. If you really just want a space before commas, you could do:
console.log(JSON.stringify(products).split('},{').join('}, {'));
http://jsfiddle.net/2MeMY/3/
Answer from dave on Stack OverflowThis may not be what you want, but if you just want it to look better, I would recommend:
console.log(JSON.stringify(products, null, 2));
which would give you
[
{
"name": "Sample Product Name"
},
{
"name": "Sample Product Name 2"
}
]
In the console. If you really just want a space before commas, you could do:
console.log(JSON.stringify(products).split('},{').join('}, {'));
http://jsfiddle.net/2MeMY/3/
If you want a json-output that is:
- Single-line
- Has spaces between prop-names and prop-values (and between items)
- Has spaces between each comma and the next prop-name/item
You can use this:
function Stringify_WithSpaces(obj) {
let result = JSON.stringify(obj, null, 1); // stringify, with line-breaks and indents
result = result.replace(/^ +/gm, " "); // remove all but the first space for each line
result = result.replace(/\n/g, ""); // remove line-breaks
result = result.replace(/{ /g, "{").replace(/ }/g, "}"); // remove spaces between object-braces and first/last props
result = result.replace(/\[ /g, "[").replace(/ \]/g, "]"); // remove spaces between array-brackets and first/last items
return result;
}
let obj = [{name: "Sample Product Name"}, {name: "Sample Product Name 2"}];
console.log("Stringified with spaces: " + Stringify_WithSpaces(obj));
And here's the function as a one-line expression:
JSON.stringify(obj, null, 1).replace(/^ +/gm, " ").replace(/\n/g, "").replace(/{ /g, "{").replace(/ }/g, "}").replace(/\[ /g, "[").replace(/ \]/g, "]")
Extended Typescript version
Here's a more verbose version (in Typescript) with options:
export class ToJSON_WithSpaces_Options {
insideObjectBraces = false;
insideArrayBrackets = false;
betweenPropsOrItems = true;
betweenPropNameAndValue = true;
}
export function ToJSON_WithSpaces(obj, options?: Partial<ToJSON_WithSpaces_Options>) {
options = Object.assign({}, new ToJSON_WithSpaces_Options(), options);
let result = JSON.stringify(obj, null, 1); // stringify, with line-breaks and indents
result = result.replace(/^ +/gm, " "); // remove all but the first space for each line
result = result.replace(/\n/g, ""); // remove line-breaks
if (!options.insideObjectBraces) result = result.replace(/{ /g, "{").replace(/ }/g, "}");
if (!options.insideArrayBrackets) result = result.replace(/\[ /g, "[").replace(/ \]/g, "]");
if (!options.betweenPropsOrItems) result = result.replace(/, /g, ",");
if (!options.betweenPropNameAndValue) result = result.replace(/": /g, `":`);
return result;
}
Ideally, this sort of function will apply the regular-expressions prior to removing the line-breaks (so that it can guarantee it's not modifying text within user-supplied strings), but I'll leave that for someone else to do since the above is sufficient for my use-case (and I think most others).
Videos
JSON.stringify({a: 3, b: {c: 3}}, null, " ");
"{
"a": 3,
"b": {
"c": 3
}
}"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify