Convert an object to a JSON-formatted string.
Syntax ConvertTo-Json [-InputObject] Object [-Compress] [-Depth Int32] [CommonParameters] Key -InputObject Object The objects to convert to JSON format. Enter a variable that contains the objects, or type a command or expression that gets the objects. You can also pipe an object to ConvertTo-Json. The InputObject parameter is required, but its value can be null ($Null) or an empty string. When the input object is $Null, ConvertTo-Json does not generate any output. When the input object is an empty string, ConvertTo-Json returns an empty string. -Compress Omit white space and indented formatting in the output string. -Depth Int32 How many levels of contained objects are included in the JSON representation. The default value is 2.
Standard Aliases for ConvertTo-Json: none, but if you want to add a short alias like ctjn, set it with set-alias
This cmdlet was introduced in Windows PowerShell 3.0.
Convert a GregorianCalendar Calendar object to a JSON string:
PS C:\> (Get-UICulture).Calendar | ConvertTo-Json { "MinSupportedDateTime": "\/Date(-62135568000000)\/", "MaxSupportedDateTime": "\/Date(253402300799999)\/", "AlgorithmType": 1, "CalendarType": 1, "Eras": [ 1 ], "TwoDigitYearMax": 2029, "IsReadOnly": false }
Show the effect of using the -Compress parameter of ConvertTo-Json. The compression affects only the appearance of the string, not its validity:
PS C:\> @{Account="User64";Domain="windevclusterdom";Admin="True"} | ConvertTo-Json -Compress {"Admin":"True","Account":"User64","Domain":"windevclusterdom"}
Convert a date object to a JSON string and then into a JSON object:
PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json
PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json
“you may live to see man-made horrors beyond your comprehension” ~ Nikola Tesla, 1898
ConvertFrom-Json - Convert a JSON-formatted string to a custom object.