Skip to content

Use prettier formatting for ConvertTo-Json #2736

Closed

Description

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

Activity

  1. added
    WG-Cmdlets
    Resolution-Fixed
    Up-for-Grabs
    and removed
    Resolution-Fixed
    on Nov 20, 2016
  2. joeyaiello commented on Nov 21, 2016

    @joeyaiello
    Contributor

    Steve Lee (@SteveL-MSFT) Hemant Mahawar (@HemantMahawar) are you guys okay adding 6.0.0 milestone to this one?

  3. added this to the milestone on Nov 21, 2016
  4. SteveL-MSFT commented on Nov 21, 2016

    @SteveL-MSFT
    Member

    Joey Aiello (@joeyaiello) considering we're targeting cloud/REST scenarios, it makes sense

  5. modified the milestones: , on Dec 19, 2016
  6. removed
    Up-for-Grabs
    on Jan 31, 2017
  7. joeyaiello commented on Mar 1, 2017

    @joeyaiello
    Contributor

    Got fixed with that commit ^

  8. daxian-dbw commented on Mar 8, 2017

    @daxian-dbw
    Member

    Mike Richmond (@mirichmo), is this issue fixed by #2787? If so, please close this issue.

  9. added
    Resolution-Fixed
    Issue-Enhancement
    on Mar 15, 2017
  10. hamidshahid commented on Oct 6, 2017

    @hamidshahid

    I see that this was done? Can we have an sample of how to use the Formatting parameter please.

  11. SteveL-MSFT commented on Oct 7, 2017

    @SteveL-MSFT
    Member

    Hamid shahid (@hamidshahid) there's no new parameter, the output should just be prettier:

    get-module pester -listavailable | convertto-json
  12. hamidshahid commented on Oct 7, 2017

    @hamidshahid

    Thanks Steve, I was expecting formatting to be similar to the one we get with Formatting.Indented option in NewtonSoft Json library but it's cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-EnhancementResolution-FixedWG-Cmdlets

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions