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

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


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


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])