EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell JSON Format
 

PowerShell JSON Format

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated March 4, 2023

PowerShell JSON Format

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

PowerShell JSON Format 1

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:

PowerShell JSON Format 2

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:

PowerShell JSON Format 3

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:

PowerShell JSON Format 4

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:

PowerShell JSON Format 5

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:

example 1

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:

example 1-1

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:

example 2

After converting to an array,

Get-Date  | ConvertTo-Json -AsArray

Output:

example 2-1

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:

example 3

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:

example 3-1

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:

example 4

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 –

  1. PowerShell Invoke-Webrequest
  2. PowerShell Wait
  3. PowerShell Execution Policy
  4. PowerShell Sleep

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW