PowerShell Shots: Use JSON File as Objects

Pawan Dubey
Jun 23, 2021

As everyone is moving towards or have already moved to JSON file, lets explore PowerShell universe to play around JSON file.

To start with just read the file using Get-Content command and store it in a string variable.
(The file I’m using can be downloaded from here)

$fileContent = Get-Content -Path “C:\Users\pawan\Desktop\example_2.json"

At this moment, if you print the value of $fileContent, you would see the values as string. Now lets convert this plain text to PowerShell objects using below command.

$jsonObject = $fileContent | ConvertFrom-Json

And now if you print the variable $jsonObject, it would be like any other object in PowerShell. Apparently you can also convert any PowerShell object to JSON by using ConverTo-Json.

--

--

Pawan Dubey
0 Followers

Exploring the universe of information technology :)