Skip to content

Instantly share code, notes, and snippets.

@wgross
Created January 27, 2017 13:57
Show Gist options
  • Save wgross/f7504f5d80863fefb6148f70ca586bca to your computer and use it in GitHub Desktop.
Save wgross/f7504f5d80863fefb6148f70ca586bca to your computer and use it in GitHub Desktop.
Format-Json witthout Newtonsoft.Json
# 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