JSON
JavaScript Object Notation (JSON), is similar to XML in some respects. It is intended to be both human and machine readable, and is written in plain text.
Much similar as a hashtable, JSON-formatted objects are made up of key and value pairs. For example:
{ "key1": "value1", "key2": "value2" }
ConvertTo-Json
The ConvertTo-Json
command can be used to convert a PowerShell object (or hashtable) to JSON:
PS> Get-Process -Id $PID |
Select-Object Name, Id, Path |
ConvertTo-Json
{
"Name": "powershell_ise",
"Id": 3944,
"Path": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe"
}
By default, ConvertTo-Json
will convert objects to a depth of two. Running the following code will show how the value for three is simplified as a string:
@{ one = @{ # 1st iteration two = @{ # 2nd iteration three = @{ four = 'value' } } } } | ConvertTo-Json
The property three is present, but the value is listed as System.Collections...