Write JSON Object to a File in PowerShell

  1. PowerShell Custom Objects
  2. PowerShell JSON Object to JSON String
  3. Save JSON String to a File

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.

Related Article - PowerShell JSON

  • Create JSON Array in PowerShell
  • Read JSON Files in PowerShell
  • Parse JSON Files Using PowerShell