How to load a JSON file and convert it to an object of a specific type?

JsonPowershellDeserializationPowershell 3.0

Json Problem Overview


I have a type FooObject and I have a JSON file which was serialized from a FooObject instance. Now I want to use ConvertFrom-Json to load the JSON file to memory and covert the output of the command to a FooObject object, and then use the new object in a cmdlet Set-Bar which only accept FooObject as the parameter type.

But I notice that the output type of ConvertFrom-Json is PSCustomObject and I did not find any way to convert PSCustomObject to FooObject.

Json Solutions


Solution 1 - Json

Try casting the custom object to FooObject:

$foo = [FooObject](Get-Content 'C:\path\to\your.json' | Out-String | ConvertFrom-Json)

If that doesn't work, try constructing the FooObject instance with the properties of the input object (provided the class has a constructor like that):

$json = Get-Content 'C:\path\to\your.json' | Out-String | ConvertFrom-Json
$foo = New-Object FooObject ($json.Foo, $json.Bar, $json.Baz)

If that also doesn't work you need to create an empty FooObject instance and update its properties afterwards:

$json = Get-Content 'C:\path\to\your.json' | Out-String | ConvertFrom-Json
$foo = New-Object FooObject
$foo.AA = $json.Foo
$foo.BB = $json.Bar
$foo.CC = $json.Baz

Solution 2 - Json

Based on PowerTip: Convert JSON File to PowerShell Object, you can do the following:

Get-Content -Raw -Path <jsonFile>.json | ConvertFrom-Json

Solution 3 - Json

I realize this is an old post, but I found a more efficient way of doing this, if casting it doesn't work. Definitely try casting it first. Casting will work as long as your class doesn't contain nested collections of custom types. Say your class looks like the following.

class Container 
{
    [string] $Id
    [string] $Name
    [System.Collections.Generic.List[Process]] $Processes
}
class Process
{
    [string] $Id
    [string] $Name
}

ConvertFrom-Json would convert it to a [PSCustomObject] but would make the List[Process] into an Object[] which would cause any cast operation to throw the following exception.

> Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Collections.Generic.List`1[Process]". > > ConvertToFinalInvalidCastException

Use the following to deserialize this type of hierarchy.

$serializer = [System.Web.Script.Serialization.JavaScriptSerializer]::new()

$content = $serializer.Deserialize((Get-Content -Path $JsonFilePath), [YourCustomType])

The [System.Web.Script.Serialization.JavaScriptSerializer] is how ConvertFrom-Json works in the background. So, I just created a new instance of that and was able to convert a multi-level (four levels to be exact and each level had a collection of the level below it) json file into my powershell class easily. I also realize that this could be simplified into the following, but it is easier to read above.

$content = [System.Web.Script.Serialization.JavaScriptSerializer]::new().Deserialize((Get-Content -Path $JsonFilePath), [YourCustomType])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNonStaticView Question on Stackoverflow
Solution 1 - JsonAnsgar WiechersView Answer on Stackoverflow
Solution 2 - JsonthatOneGuyView Answer on Stackoverflow
Solution 3 - JsonPatrick McvayView Answer on Stackoverflow