EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell JSON Format
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

PowerShell JSON Format

By Chirag NagarekarChirag Nagarekar

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more