11

I want to print JSON.stringify()'d objects to the console, for context as part of a Mocha test suite output.

As the tests indent, I'd like the object log lines to be indented far enough rightwards (say, 3-4 tab spaces) that they are recognisably in the right describe() group.

How could I achieve this with something like console.log or process.stdout.write?

4
  • Look at the extra parameters of JSON.stringify on the MDN. Commented Sep 12, 2015 at 16:15
  • 1
    @MinusFour That leaves the initial curly brackets at column 0, and creates spaces at all depths of the object. I want a normal JSON.stringify(obj,null,2) output, all shifted right by three tabspaces. Commented Sep 12, 2015 at 16:53
  • 1
    Then you want to shift the whole string by 3 tabspaces? Commented Sep 12, 2015 at 16:57
  • @MinusFour - yes. Every line needs to shift. Commented Sep 12, 2015 at 16:58

3 Answers 3

15

If just used for console.log you might want to look up for groups, check this on your console:

var obj = {
  a : 1,
  b : 2,
  c: 3
  }

console.log('Non-tabbed');
console.group();
console.log(JSON.stringify(obj, null, 2));
console.groupEnd();
console.log('Back to non-tabbed');

Works fine on current browsers and latest node. There's also a package on npm that might just work for that if you are working with older node versions.

node-console-group

This shifts the whole JSON string by 3 spaces. It breaks the JSON string by new lines, then adds on each line 3 spaces to a new string which will hold every line shifted.

var results = document.getElementById('results');
var obj = {
    a: 5,
    b: 3,
    c: 4
};

var string = JSON.stringify(obj, null, 2);

var stringShifted = '';
string.split('\n').forEach(function(line){
    stringShifted += '   ' + line + '\n';
});
console.log(string);
console.log(stringShifted);

results.innerHTML = 'Before : \n' + string;
results.innerHTML += '\n\nAfter : \n' + stringShifted;
<pre id="results"></pre>

Sign up to request clarification or add additional context in comments.

3 Comments

This answer is wrong. Node.JS has console.group since v8.5.0.
@Utku added clarification.
If I were you, I would just remove that paragraph and link to that package. It works natively now, there is no point of complicating things.
0

You can add a short reg-ex replace on the JSON.stringify output to get the effect that you're after.

var myObject = _buildAnObject();   

it("Should log my entire JSON.stringify indented", function(){
   console.log("My log message: \n\t" 
        + JSON.stringify(myObject, null, 2).replace(/\n\r?/g, '\n\t'));
   assert(true);
});

I've put a working mocha example on codepen.io.

Comments

-2

under nodejs you can try util.inspect(obj, {depth:10, colors:true ...}) i used it under mocha tested to indent logs

const log = (...args:unknown[])=>{
    setTimeout(()=>{
        let indent = "\t";
        args = args.map(a=>util.inspect(a, {colors:true, depth:10}).replace(/^\x1B\[32m['"`]/ig, '\x1B[32m').replace(/['"`]\x1B\[39m$/ig, '\x1B[39m').replace(/\n/g, `\n${indent} `))
        console.log.call(console, indent, ...args);
    })
}
let user = {
    name:"surinder singh",
    age:38,
    hobbies:[{
        name:"Something interesting",
        from:"birth"
    },{
        name:"Electronic DIY",
        from:"from ~primary"
    },{
        name:"Coding",
        from:"after school"
    }]
}
let args = ["a string 1234", 123 , {xyz:"xyz", num:123, arr:["xxx", 123, {345:"xxx"}, user], bool:true}];

console.log( ...args );
log( ...args  )

enter image description here

2 Comments

According to the docs, the depth argument doesn't do what you think it does: > "The depth argument is the number of levels deep into a nested object to recurse - it defaults to 2. Setting it to null will cause it to recurse 'all the way', showing every level."
i am indenting using .replace(/\n/g, '\n${indent} ') not with depth option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.