NonStatic
NonStatic

Reputation: 1081

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

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.

Upvotes: 69

Views: 187865

Answers (4)

Patrick Mcvay
Patrick Mcvay

Reputation: 2281

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

Upvotes: 8

Jaie
Jaie

Reputation: 117

Class FooObject{
    FooObject([string]$FileLocation) { 
        $Properties = ConvertFrom-Json (Get-Content -Raw $FileLocation);
        $this.Init($Properties);
    }
    [void] Init([object]$Properties) {
        foreach ($Property in $Properties.PSObject.Properties) {
            if([bool]($this.PSobject.Properties.name -match $Property.Name)){
                $this."$($Property.Name)" = $Property.Value;
            }
        }
    }
}

I found this worked well for me hope it helps someone. Also the init method in this could technically be used to convert any object to the new type.

Example Usage

 $x = [FooObject]::new("Config.json");

Upvotes: 0

thatOneGuy
thatOneGuy

Reputation: 10612

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

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

Upvotes: 25

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

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

Upvotes: 79

Related Questions