Author Posts

June 14, 2016 at 9:21 pm

Hi,

I am using a Web API 2.0 that gives me the return data as JSON.

The return data from the Web API is objects, which can contain list of objects (List).

In Powershell, after retrieving the data from the Web API, I would like to have the same objects as the Web API does.
How can I easily convert the JSON return data to objects of same class type as the Web API uses?

I know/have access to the classes (DTO classes) from the Web API.

I can get what I want by doing something like this:


public class Class1DTO
{
public int Id { get; set; }
public string Name { get; set; }
public System.Collections.Generic.List Accounts{ get; set; }
}

public class Class2DTO
{
public int Id { get; set; }
public string Blabla{ get; set; }
public string AccountType { get; set; }
}
"@

Add-Type -TypeDefinition $classes

And then cast the psobject to the classes..
But that seems tedious. Is there an easier way?

Best regards
Stig

  • This topic was modified 2 years, 3 months ago by  Stig Sörnsen.
  • This topic was modified 2 years, 3 months ago by  Stig Sörnsen.

June 14, 2016 at 9:35 pm

Classes in PowerShell are only in v5, and they're not quite up to that level, yet. As you see, you have to kind of manually define them out each time.

However, have you tried just using ConvertFrom-JSON? That should give you a deserialized object that's as close as possible to the original, excepting for its TypeName, which you can change yourself if need be.

June 15, 2016 at 8:00 am

Hi Don,

Thank you for your time and answer.

I have no problem in using V5 only Powershell. The ConvertFrom-JSON is alright, but not with nested objects.

I got the conversion in place, now it is just a matter of displaying properly.

Please see this example:

class ParentClass
{
[string]$Name
[ChildClass]$Child
}

class ChildClass
{
[string]$Name
[string]$Something
}

$ChildObject = New-Object -TypeName ChildClass
$ChildObject.Name = "Child"
$ChildObject.Something = "Awesome"

$ParentObject = New-Object -TypeName ParentClass
$ParentObject.Name = "Parent"
$ParentObject.Child = $ChildObject

That gives me this:

PS C:\> $ParentObject

Name Child
---- -----
Parent ChildClass

But I would like to display the Name property from the ChildObject instead of "ChildClass".

How can I accomplish that?

  • This reply was modified 2 years, 3 months ago by  Stig Sörnsen.

June 15, 2016 at 10:42 am

I think that's because the child class inherits ToString from Object. This method returns the name of the class.

Just override it (I've also done other minor modifications to your example):

class ParentClass {
    [string]$Name
    [ChildClass]$Child
}

class ChildClass {
    [string]$Name
    [string]$Something

    [string] ToString() {
        return $this.Name
    }
}

$ChildObject = [ChildClass]::new()
$ChildObject.Name = "Bobby"
$ChildObject.Something = "Awesome"

$ParentObject = [ParentClass]::new()
$ParentObject.Name = "Jack"
$ParentObject.Child = $ChildObject

$ParentObject

This gives you:
Name Child
—- —–
Jack Bobby

  • This reply was modified 2 years, 3 months ago by  Paal Braathen.

June 15, 2016 at 10:45 am

Beautiful Paal!

Exactly what I needed. Thank you very much