Last active
December 12, 2024 00:59
-
-
Save mdnmdn/6936714 to your computer and use it in GitHub Desktop.
ConvertTo-JSON for Powershell 2.0
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
function Escape-JSONString($str){ | |
if ($str -eq $null) {return ""} | |
$str = $str.ToString().Replace('"','\"').Replace('\','\\').Replace("`n",'\n').Replace("`r",'\r').Replace("`t",'\t') | |
return $str; | |
} | |
function ConvertTo-JSON($maxDepth = 4,$forceArray = $false) { | |
begin { | |
$data = @() | |
} | |
process{ | |
$data += $_ | |
} | |
end{ | |
if ($data.length -eq 1 -and $forceArray -eq $false) { | |
$value = $data[0] | |
} else { | |
$value = $data | |
} | |
if ($value -eq $null) { | |
return "null" | |
} | |
$dataType = $value.GetType().Name | |
switch -regex ($dataType) { | |
'String' { | |
return "`"{0}`"" -f (Escape-JSONString $value ) | |
} | |
'(System\.)?DateTime' {return "`"{0:yyyy-MM-dd}T{0:HH:mm:ss}`"" -f $value} | |
'Int32|Double' {return "$value"} | |
'Boolean' {return "$value".ToLower()} | |
'(System\.)?Object\[\]' { # array | |
if ($maxDepth -le 0){return "`"$value`""} | |
$jsonResult = '' | |
foreach($elem in $value){ | |
#if ($elem -eq $null) {continue} | |
if ($jsonResult.Length -gt 0) {$jsonResult +=', '} | |
$jsonResult += ($elem | ConvertTo-JSON -maxDepth ($maxDepth -1)) | |
} | |
return "[" + $jsonResult + "]" | |
} | |
'(System\.)?Hashtable' { # hashtable | |
$jsonResult = '' | |
foreach($key in $value.Keys){ | |
if ($jsonResult.Length -gt 0) {$jsonResult +=', '} | |
$jsonResult += | |
@" | |
"{0}": {1} | |
"@ -f $key , ($value[$key] | ConvertTo-JSON -maxDepth ($maxDepth -1) ) | |
} | |
return "{" + $jsonResult + "}" | |
} | |
default { #object | |
if ($maxDepth -le 0){return "`"{0}`"" -f (Escape-JSONString $value)} | |
return "{" + | |
(($value | Get-Member -MemberType *property | % { | |
@" | |
"{0}": {1} | |
"@ -f $_.Name , ($value.($_.Name) | ConvertTo-JSON -maxDepth ($maxDepth -1) ) | |
}) -join ', ') + "}" | |
} | |
} | |
} | |
} | |
#"a" | ConvertTo-JSON | |
#dir \ | ConvertTo-JSON | |
#(get-date) | ConvertTo-JSON | |
#(dir \)[0] | ConvertTo-JSON -maxDepth 1 | |
#@{ "asd" = "sdfads" ; "a" = 2 } | ConvertTo-JSON |
why you skipped "|UInt64|UInt16" in row "'Int32|Double' {return "$value"}"?
without adding it, this results to generate different json than if we use ms convertto-json commandlet
for example if we use
$Disks= @(Get-WmiObject Win32_DiskDrive | Select-Object -Property Caption, Partitions, Size)
$Volumes= @(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -ne 4 } | Select-Object -Property DeviceID, Size, FreeSpace)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
how can i remove \t in json?
{\t"id": "63304D56-6A42-43DB-7670-B73FD8389E6", \t"type": "Hello"}