Users can download JSON files from our application, which I want to prettify for easier debugging than if all is in one line. However this increases the file size by nearly 40% even if the indentation is just a single tab or space.
As a compromise, I want to exclude all values with e.g. the key "large" from prettification, like this:
{
"small":
{
"a": 1,
"b": 2,
"large": {"a":1,"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
},
"large": {"a":1,"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
}
I tried to solve this using the replacer parameter of JSON.stringify:
JSON.stringify(data,(key,value)=>key==="large"?JSON.stringify(data):value,'\t');
However the values for the "large" keys ends up escaped:
data =
{
"small":
{
"a": 1,
"b": [2,3,4,5],
"large": {"myarray":[1,2,3,4],"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
},
"large": {"a":1,"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
}
const json = JSON.stringify(data,(key,value)=>key==="large"?JSON.stringify(data):value,'\t');
document.getElementById("json").innerText = json;
<html>
<body>
<pre id="json">
</pre>
</body>
</html>
How can I prevent this escaping from happening or otherwise partially prettify JSON in JavaScript?
Every time you call
JSON.stringify
on an object (or part of an object) more than once, you're going to end up with escaped speech marks. In this case, after the innerJSON.stringify
function has been applied inside the replacer function, there is no way to tell the outerJSON.stringify
function not to treat that instance as it would any other string.So, I think if your goal is to 'partially' stringify an object, you need to write a function that implements that. For this problem, I would suggest something like this:
Here, a recursive function is used, which loops through the keys of an object and appends the key-value pair to a string, handling for different data types. If the
key
is found in theexclude
array, then it is stringified.By looping through the keys and conditionally applying
JSON.stringify
where required, you are effectively avoiding the above issue.I'm sure there may be edge cases to watch out for when writing the JSON yourself instead of using a built-in function, but you could adapt something like this to suit your specific needs.
EDIT: answer updated to better consider arrays within the object.