JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. In this PowerShell tutorial, I will explain how to read a JSON file into an array in PowerShell.
To read a JSON file into an array in PowerShell, use the Get-Content cmdlet to retrieve the file content and pipe it to the ConvertFrom-Json cmdlet. For example, $array = Get-Content -Path ‘file.json’ | ConvertFrom-Json will parse the JSON file into a PowerShell array if the JSON is an array format.
Understanding JSON
Before we dive into PowerShell, let’s understand the structure of a JSON file. A JSON file typically consists of key-value pairs in a structured format. It can contain objects (denoted by curly braces {}) and arrays (denoted by square brackets []). Here’s a simple example of a JSON file:
{
"users": [
{"name": "John", "age": 30},
{"name": "Jane", "age": 25},
{"name": "Doe", "age": 22}
]
}In this example, users is an array of objects, each containing a name and an age.
Reading JSON into PowerShell Array
To read JSON data into an array in PowerShell, we use the ConvertFrom-Json cmdlet, which converts JSON formatted strings to PowerShell objects.
Method 1: Using Get-Content and ConvertFrom-Json
The most common approach to read a JSON file into an array in PowerShell is to use Get-Content to get the file’s content and then pipe it to ConvertFrom-Json.
Here’s a step-by-step example:
- Save your JSON file. For this example, we’ll name it
users.json. - Open PowerShell and enter the following command:
$jsonData = Get-Content -Path 'C:\Bijay\users.json' | ConvertFrom-Json- Now,
$jsonDatacontains the JSON file’s content as a PowerShell object. To access theusersarray, you can simply use$jsonData.users.
$usersArray = $jsonData.users- You can loop through the array and access each user’s properties like so:
foreach ($user in $usersArray) {
Write-Host "Name: $($user.name), Age: $($user.age)"
}You can see the output after I executed the PowerShell script using VS code.
$jsonData = Get-Content -Path 'C:\Bijay\users.json' | ConvertFrom-Json
$usersArray = $jsonData.users
foreach ($user in $usersArray) {
Write-Host "Name: $($user.name), Age: $($user.age)"
}
Method 2: Using ConvertFrom-Json Directly
Another way to read a JSON file in PowerShell is to use ConvertFrom-Json directly with the -InputObject parameter.
$jsonContent = Get-Content -Path 'C:\path\to\your\users.json' -Raw
$jsonData = $jsonContent | ConvertFrom-JsonThe -Raw parameter ensures that Get-Content reads the file as a single string, which is necessary for ConvertFrom-Json to parse the JSON structure properly.
Method 3: Handling JSON with Custom Objects
Sometimes, you might want to convert JSON data directly into custom PowerShell objects. This can be done by defining a class in PowerShell and then using ConvertFrom-Json to cast the JSON directly into objects of that class.
class User {
[string]$name
[int]$age
}
$jsonData = Get-Content -Path 'C:\path\to\your\users.json' | ConvertFrom-Json
$usersArray = @($jsonData.users | ForEach-Object {
$_ | ConvertTo-Json | ConvertFrom-Json -As [User]
})In this example, ConvertTo-Json is used to convert each user to a JSON string, which is then piped to ConvertFrom-Json with the -As parameter specifying the custom class User.
Tips:
- Always use the
-Rawparameter withGet-Contentwhen reading JSON files to ensure the entire file is read as a single string. - Use
Write-HostorWrite-Outputto print the contents of the array to the console for debugging. - PowerShell is case-insensitive, but JSON keys are case-sensitive. Make sure to match the case when referring to JSON keys.
- Validate your JSON file with a JSON validator to ensure it’s correctly formatted before trying to parse it with PowerShell.
Conclusion
Reading JSON data into an array in PowerShell is a straightforward process. Whether you’re using Get-Content and ConvertFrom-Json together or handling JSON data with custom objects, PowerShell provides the flexibility to work with JSON files.
In this PowerShell tutorial, I have explained how to read a JSON file into an array in PowerShell
You may also like:
- How To Read File Line By Line Into Array In PowerShell?
- How To Write Array To File In PowerShell?
- How to Read Excel Files into an Array in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.