[SOLVED] How do I print human readable contents of JSON in console?

Where my JSON is called ‘test’ I have tried the following:

console.log(this.test);//Prints [object object]

console.log(this.test.resource);//Prints ‘undefined’

Ho can I print the JSON in a human readable format?
Cheers

SOLVED:

console.log(JSON.stringify(this.test,null,'\t'));

WARNING!
Its worth noting that if any of your values in the JSON become ‘undefined’ for whatever reason, the console won’t display them AT ALL!! It won’t even display yourValue = undefined. It will just completely skip that key/value pair and move to the next one.

If you check API, you can notice that you could use a replacer function to incude your undefined keys. Something like this:

const replace = (k, v) => { return v === undefined ? null : v; };
JSON.stringify(this.test, replace, '\t');
2 Likes

Great. Thanks!