Created
January 27, 2017 13:57
-
-
Save wgross/f7504f5d80863fefb6148f70ca586bca to your computer and use it in GitHub Desktop.
Format-Json witthout Newtonsoft.Json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Taken from: https://github.com/PowerShell/PowerShell/issues/2736 | |
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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment