question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use prettier formatting for ConvertTo-Json

See original GitHub issue

The output from ConvertTo-Json is pretty ugly at the moment:

@{
  foo = @{
    first = 'a'
    second = 'bbbbbbbb'
  }
  barbarbarbar = @{
    first = 'a'
    second = 'bbbbbbbb'
  }
  dan = 15
} | ConvertTo-Json

Results in:

{
    "dan":  15,
    "foo":  {
                "second":  "bbbbbbbb",
                "first":  "a"
            },
    "barbarbarbar":  {
                         "second":  "bbbbbbbb",
                         "first":  "a"
                     }
}

The formatting is all over the place. On the other hand, pretty much every other JSON library will produce something like this:

{
  "dan": 15,
  "foo": {
    "second": "bbbbbbbb",
    "first": "a"
  },
  "barbarbarbar": {
    "second": "bbbbbbbb",
    "first": "a"
  }
}

The standard JSON.NET pretty printing (Newtonsoft.Json.Formatting.Indented) may be useful, as it formats the JSON “correctly”.

This is my current workaround to properly formatting JSON in PowerShell, it’s a bit messy though:

# Formats JSON in a nicer format than the built-in ConvertTo-Json does.
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
  $indent = 0;
  ($json -Split '\n' |
    % {
      if ($_ -match '[\}\]]') {
        # This line contains  ] or }, decrement the indentation level
        $indent--
      }
      $line = (' ' * $indent * 2) + $_.TrimStart().Replace(':  ', ': ')
      if ($_ -match '[\{\[]') {
        # This line contains [ or {, increment the indentation level
        $indent++
      }
      $line
  }) -Join "`n"
}

(usage is like $foo | ConvertTo-Json | Format-Json)

This looks consistent across both PowerShell 5 and 6

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.14965.1001
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14965.1001
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.12
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:45
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
joeyaiellocommented, Nov 21, 2016

@SteveL-MSFT @HemantMahawar are you guys okay adding 6.0.0 milestone to this one?

1reaction
SteveL-MSFTcommented, Oct 7, 2017

@hamidshahid there’s no new parameter, the output should just be prettier:

get-module pester -listavailable | convertto-json
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prettier Code Formatter Setup Guide
Tutorial for setting up prettier in your JavaScript web applications for automatic layout formatting to save time and effort.
Read more >
Prettify json in powershell 3
Format -XML is a PSCX command, and I believe it does the same thing by converting a string to an XML object then...
Read more >
Stop re-formatting package.json with Prettier and VSCode ...
You are using VSCode with Prettier plugin to format all the stuff, so you don't have to ever argue with anyone during code...
Read more >
Configuration File
Prettier uses cosmiconfig for configuration file support. This means you can configure Prettier via (in order of precedence): A "prettier" key in your...
Read more >
JSON and ARM Templates
Add the Pretty Json Extension for formatting JSON docs. ... PowerShell – ConvertTo-Json and ConvertFrom-Json can import JSON as a PowerShell ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found