Updated March 4, 2023
Definition of PowerShell JSON Format
JSON format also stands for JavaScript Object Notation Format which is an Open Standard Format and in Human readable format and mainly used for faster communication between the browser and the client because its ease for processing and can be retrieved as PowerShell data using Web commands like Invoke-WebRequest and Invoke-RestMethod. Despite its name, this language is quite different than the JavaScript format.
Syntax:
JSON language uses the below syntax.
{
"String":"Value"
}
The above is the simple syntax of the JSON object format. It has the property header and the value and both are separated by the colon ( : ) which means that the particular header has that particular value.
JSON array Syntax,
{
"String": [
Value1,
Value2
]
}
The above syntax shows that the String property header has two array values, Value1 and Value2.
Complex JSON structure,
{
"String":[
{
"String1":"Value1",
"String2":"Value2"
},
{
"String3":"Value3",
"String4":[
"Value4",
"Value5"
]
}
]
}
The above syntax shows the complex structure of the JSON format where it has a combination of the String and its values, arrays, and sub-strings and values.
How does the JSON type work in PowerShell?
JavaScript Object Notation or JSON files have the .json syntax. As we have seen in the above syntax section that the JSON is a String and a value pair and there are several such strings and values are associated with each other and it also comprises the array and subgroups of strings and values and can create a complex structure.
You can write a JSON file in any editor as it is a simple text format but to create an error-free JSON file, you can use the VS Code because it shows the error when there is any syntax missing. Second thing, once you are done with creating a JSON file, you can validate the JSON file online as well. Several websites validate the JSON file but https://jsonlint.com/ is the popular website to validate your JSON file.
Once the JSON file is created and validated, our task is to read the JSON file in PowerShell. PowerShell uses the two cmdlets ConvertTo-JSON and ConvertFrom-JSON to work with JSON files.
The ConvertTo-JSON cmdlet converts any possible output to the JSON format and the ConvertFrom-JSON cmdlet converts the JSON input to the custom Object or the hashtable format. First, we will check the sample input JSON file created above and how we can use the ConvertFrom-JSON command.
{
"Company":"AeroSpace",
"HeadOffice":"Russia",
"BranchOffices":[
"India",
"Australia"
]
}
If you are directly working with the JSON file then you can use the below command to get the output into the hashtable format.
Get-Content .\Test1.json | ConvertFrom-Json
Output:
Or you can use the below command directly without saving the file as JSON.
$jsoninput = '{
"Company":"AeroSpace",
"HeadOffice":"Russia",
"BranchOffices":[
"India",
"Australia"
]
}'
$jsoninput | ConvertFrom-Json
Output:
To convert any output to the JSON format you need to use the ConvertTo-JSON command as shown below.
Get-Process notepad++ | Select Name, id, WorkingSet, CPU | ConvertTo-Json
Output:
In JSON structure, Boolean values $true and $false are defined as true or false respectively while $null is defined as null. See the example below.
Get-ChildItem -Path C:\Temp\25Aug2020.txt | ConvertTo-Json
Output:
You can see in the above output that null and true are defined and also the Date format is mentioned with /Date and the path is defined in a double backslash (“\\”) rather than a single backslash.
Examples
1. Converting Command output to the JSON file.
We can convert almost any command output to the JSON format using the ConvertTo-JSON pipeline command. For example,
Get-Process chrome | Select Name, ID, WorkingSet, CPU, PagedMemorySize64 | Select -First 3 | ConvertTo-Json
Output:
You can also compress the output using the -Compress parameter so that the output will be displayed in a single line as shown below.
Get-Process chrome | Select Name, ID, WorkingSet, CPU, PagedMemorySize64 | Select -First 3 | ConvertTo-Json -Compress
Output:
2. Converting JSON output to the array.
To convert the JSON output to the array, we need to use -AsArray parameter.
Please note: This parameter is only supported in the .Net Core versions (PowerShell 6.0 and above) but below 6.0 version (.Net Framework versions) it is not supported.
Without Converting to an array,
Get-Date | ConvertTo-Json
Output:
After converting to an array,
Get-Date | ConvertTo-Json -AsArray
Output:
There will be square brackets will be added when you convert JSON output to an array.
3. Using the Invoke-Webrequest
You can leverage JSON commands to work with the website output data. For example, we have a URL https://www.reddit.com/r/todayilearned/top.json?limit=100 which shows the Reddit top 100 posts from the particular page in a JSON format.
Invoke-WebRequest -Uri "https://www.reddit.com/r/todayilearned/top.json?limit=100" | ConvertFrom-Json
Output:
To read the titles of those posted contents,
$Out = Invoke-WebRequest -Uri "https://www.reddit.com/r/todayilearned/top.json?limit=100" | ConvertFrom-Json
$out.data.children.data | Select Title
Output:
The above command is similar to the RestAPI command which automatically formats the web JSON output data without using external ConvertFrom-JSON command.
Invoke-RestMethod -Uri "https://www.reddit.com/r/todayilearned/top.json?limit=100"
4. JSON commands to convert the output into HashTable
With the JSON commands, we can convert the output of the cmdlets directly to the hashtable as shown below.
Get-Service | ConvertTo-Json | ConvertFrom-Json
Output:
You can also select few fields,
Get-Service | Select Name, Starttype, Status | ConvertTo-Json | ConvertFrom-Json
Conclusion
JSON format has surpassed the use of the XML format because it is easier to deal with it and it is quicker to load for the browsers for the websites which use the heavy contents and this format is much lighter than the XML format. JSON files are also useful in the Infrastructure development automation by leveraging Infrastructure as a Code (IaaS) format and used by the cloud technologies as well mainly Azure to work with the Azure DevOps and ARM templates for deploying infrastructure resources as bulk and quickly.
Recommended Articles
This is a guide to PowerShell JSON Format. Here we discuss the definition, syntax, parameters, How does the JSON type work in PowerShell? and example with code implementation. You may also have a look at the following articles to learn more –