PowerShell Custom Object to and from JSON

Imagine a situation where you need to keep some complex data and want to be able to work with it in PowerShell. Traditionally, we used XML but JSON is much lighter way to do it.

There are situations where you want to deal with complex data within your script. Let’s try a basic PSCustomObject.

$data = [PSCustomObject]@{
    attr1 = "value1"
    attr2 = "value2"
    attr3 = @{
        attr4 = "value4"
        attr5 = "value5"
    }
}

Write-Host $data

When you execute the code above, you will see an output like the following.

@{attr1=value1; attr2=value2; attr3=System.Collections.Hashtable}

So it’s quite easy to nest the object. Let’s see if we can have a collection.

# sample2
$data2 = [PSCustomObject]@{
    attr1 = "value1"
    attr2 = "value2"
    attr3 = @(
        "val1", "val2", "val3"
    )
}

Write-Host $data2

Executing the code will give us an output like the following.

@{attr1=value1; attr2=value2; attr3=System.Object[]}

This example may look useless but it is useful in a sense that you can deal with parameters as one object which contains complex data. To make it more useful, we can serialize and deserialize the data to and from JSON file. Let’s give it a try. I will use the second sample in this article to generate JSON file from the PSCustomObject.

# sample3
$data3 = [PSCustomObject]@{
    attr1 = "value1"
    attr2 = "value2"
    attr3 = @(
        "val1", "val2", "val3"
    )
}

$data3 | ConvertTo-Json | Set-Content -Path "data3.json"

$data3 object is carried over the pipe and converted to JSON format and then the string data is yet again carried over the second pipe and saved into disk by Set-Content command. The content of data3.json looks like the following.

{
    "attr1":  "value1",
    "attr2":  "value2",
    "attr3":  [
                  "val1",
                  "val2",
                  "val3"
              ]
}

If you give -Compress option to ConverTo-Json, it literally compresses the data. It’s hard to read, but it saves a lot of disk space especially when you have to deal with big data.

{"attr1":"value1","attr2":"value2","attr3":["val1","val2","val3"]}

So converting the data to JSON format and saving it to a file is half useful. The best thing is to be able to deserialize the data back to PSObject and start to work on it again. Let’s try it.

# sample 4
$data4 = Get-Content "data3.json" | ConvertFrom-Json
Write-Host $data4.attr1
Write-Host $data4.attr2
ForEach($v in $data4.attr3)
{
    Write-Host $v
}

How easy it is to retrieve data from file and create an useful object in memory in just a line!

This technique can be used for any object. I will try it on Get-ChildItem.

# sample 5
Get-ChildItem | ConvertTo-Json -Compress | Set-Content -Path "file-system-data.json"

Get-Service, Get-Process and whatever the command that returns objects can be utilized to save the data into JSON object and even deserialized from the JSON data.

4 thoughts on “PowerShell Custom Object to and from JSON”

  1. Great post. Really helped me out. One thing that you didn’t mention which was also important was the -Depth parameter.

    1. Thank you for your comment!
      I’m guessing -Depth parameter for ConvertTo-Json Cmdlet right? It’s been a long time to work on PowerShell as I work in Linux environment right now so…

  2. That’s right, for the ConvertTo-Json. Otherwise only the first array and objects will be converted to Json, the layers below will remain as PowerShell Custom Objects. While PS1 is a clear improvement to the traditional cmd, it’s nowhere near Unix. But we all have to work with what’s available.

Leave a Reply to John Cancel reply

Your email address will not be published.