Write JSON Object to a File in PowerShell

Migel Hewage Nimesha May 27, 2022
  1. PowerShell Custom Objects
  2. PowerShell JSON Object to JSON String
  3. Save JSON String to a File
Write JSON Object to a File in PowerShell

PowerShell is a very powerful object-based language that can be used to create structured data. Hence, it is easy to work with than plain text.

PowerShell Custom Objects

The PSCustomObject is the base for PowerShell objects. It contains properties and values.

Therefore, PowerShell can handle JSON objects as well.

Let’s create a JSON custom object from an example JSON. We have assigned a JSON to the $MyJsonVar variable.

$MyJsonVar = @"
 {
   "ExampleJson":{
     "Fruit1":{
       "Name":"Apple",
       "Price":"`$10.00"
     }
  }
 }
"@

Output:

PowerShell Custom Objects 1

We need to create the actual JSON object using the ConvertFrom-JSON cmdlet. This will create the real PSCustomObject.

Let’s assign the newly created JSON object to the $MyJsonObject variable.

$MyJsonObject = $MyJsonVar | ConvertFrom-Json

Let’s display the newly created PSCustomObject, a JSON object.

$MyJsonObject

Output:

PowerShell Custom Objects 2

You can access the JSON object by its properties.

$MyJsonObject.ExampleJson.Fruit1.Price
$MyJsonObject.ExampleJson.Fruit1.Name

Output:

PowerShell Custom Objects 3

So, it has been confirmed that we got a proper PowerShell custom object called $MyJsonObject.

PowerShell JSON Object to JSON String

The ConvertTo-Json cmdlet can convert an existing custom object to a JSON string. This is going to be plain text in JSON format.

Syntax:

ConvertTo-Json
              [-InputObject] <Object>
              [-Depth <Int32>]
              [-Compress]
              [-EnumsAsStrings]
              [-AsArray]
              [-EscapeHandling <StringEscapeHandling>]
              [<CommonParameters>]

All the above parameters are optional to the ConvertTo-Json cmdlet.

The -Depth parameter can specify the number of levels in the JSON string. It is an important parameter and needs to use very carefully.

The faulty use of this parameter might cause a loss of data. The default value is 2.

The -InputObject parameter specifies the custom object that needs to be converted into a JSON string. We can pipe a custom object to the ConvertTo-Json cmdlet easily.

We can send the $MyJsonObject through the pipe (|) to transform the custom object into a JSON string.

$MyJsonObject | ConvertTo-Json

Output:

JSON Object to JSON String

Save JSON String to a File

It is possible to save a JSON string to a file using PowerShell. We can pipe the JSON string output to the Out-File cmdlet.

It is possible to specify the path where we need to create the .json file.

$MyJsonObject | ConvertTo-Json | Out-File "D:\misc\example.json"

The path "D\misc\example.json" might vary. This will create an example.json file inside the given directory structure.

Output:

Saving JSON string to a file

The JSON custom object has been saved to an example.json file in JSON format.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - PowerShell JSON