Web API -> JSON -> PSObjects -> Objects of class?

Tagged: ,

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Stig Sörnsen Stig Sörnsen 3 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #42947
    Profile photo of Stig Sörnsen
    Stig Sörnsen
    Participant

    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 3 months, 2 weeks ago by Profile photo of Stig Sörnsen Stig Sörnsen.
    • This topic was modified 3 months, 2 weeks ago by Profile photo of Stig Sörnsen Stig Sörnsen.
    #42955
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    #42966
    Profile photo of Stig Sörnsen
    Stig Sörnsen
    Participant

    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 3 months, 2 weeks ago by Profile photo of Stig Sörnsen Stig Sörnsen.
    #43015
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    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 3 months, 2 weeks ago by Profile photo of Paal Braathen Paal Braathen.
    #43018
    Profile photo of Stig Sörnsen
    Stig Sörnsen
    Participant

    Beautiful Paal!

    Exactly what I needed. Thank you very much

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.