Cli-microsoft365: Prettify JSON output

Created on 22 Jan 2020  路  7Comments  路  Source: pnp/cli-microsoft365

Currently when using global 鈥攐utput json flag, the returned JSON is printed to the console on a single line,

This also means that when outputting the JSON directly to a file e.g.> file.json, the contents are also on one line.

It would be nice if the JSON was outputted as multi line for improved readability.

enhancement work in progress

All 7 comments

One question I have is changing this would change the behavior in a potentially breaking way. Given that it's possible to format the output before saving it to a file in the shell, do we want to risk breaking existing scripts/introducing a major version for this change? Alternatively, we could introduce an additional output format or a flag, like --output json --pretty to have the output formatted natively by the CLI without using external tools and changing existing behavior.

The proposed change would be to simply add white space to the existing JSON.stringify() method used in Utils.ts when the json is used as the value of the --output option in the command ...

.. line 106 would change from, return JSON.stringify(logStatement); to return JSON.stringify(logStatement, null, 2);

Would this be a breaking change? As far as I am aware, the JSON returned would still be valid and parsed in the same way as before.

If you had a tool in place that took CLI's output based on the assumption that the output is on a single line, while it would be no longer the case after this change, then it would be a breaking change.

I see your point and agree, shall we leave this here and see if there is any interest in implementing this enhancement?

Thanks for the discussion 馃憤馃徎

As you've mentioned, it would be a very simple solution. If we agreed to expose it for now behind a separate flag that would be used in conjunction with --output json, then we could proceed with implementing it. When we start discussing features for the next major version, we could consider dropping the extra flag and use the prettified output as a default.

--pretty flag implemented in https://github.com/garrytrinder/office365-cli/tree/prettify-json

@waldekmastykarz I expect that this new flag will need to be added to every command in the docs, is there an easy way to do this?

regex and VSCode 馃槉 I went through the same recently when introducing the query option for JMESPath

Was this page helpful?
0 / 5 - 0 ratings