Print out contents of nested objects

Tagged: ,

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Lawrence Keyes Lawrence Keyes 1 year, 9 months ago.

  • Author
    Posts
  • #23284
    Profile photo of Lawrence Keyes
    Lawrence Keyes
    Participant

    Trying to create a list of delivery addresses for UPS. This consists of data returned from our order system, in the form of
    ID
    AddressFullNames
    AddressLine1
    AddressLine2
    AddressLine3
    PostalCode

    I created a custom object to pull in data from other objects that are returned from our order system API .

    $Delivery = New-Object -TypeName PSObject -Property @{
    ID = $PBOrder.Response.ID
    AddressFullNames = $BPOrder.Response.Parties.Delivery.AddressFullNames
    AddressLine1 = $BP{Order.Response.Parties.Delivery.AddressLine1
    AddressLine2 = $BPOrder.Response.Parties.Delivery.AddressLine2
    AddressLine3 = $BPOrder.Response.Parties.Delivery.AddressLine3
    PostalCode = $BPOrder.Response.Parties.Delivery.PostalCode
    }

    The data structure that is returned is a custom system object. which when printed out looks like this

    PS> $DELIVERY
    ID : {68, 69, 70, 71...}
    AddressFullNames : {$null, $null, $null, $null...}
    AddressLine1 : {237 Commerce St. Suite 101, PO 58, 237 Commerce St. Suite 101, PO 58...}
    AddressLine3 : {, Saxeville, , Saxeville...}
    PostalCode : {, R0H 0M0, , R0H 0M0...}
    AddressLine2 : {Wiiliston VT 05495, , Wiiliston VT 05495, ...}

    I can access any of the items individual using dot notation.
    For example
    C:>PS $Delivery.PostalCode #But this prints out all of the postal codes.

    05495
    R0H 0M0
    R0H 0M0
    S1 4RT
    R0H 0M0
    54976

    Of course, what I want is a regular address records that look like this.

    ID : 70
    AddressFullNames : Joe Blow
    AddressLine1 : 123 Anywhere Lane
    AddressLine2 : Sometown
    AddressLine3 : VT
    PostalCode : 05446

    ID : 72
    AddressFullNames : Mary Smith
    AddressLine1 : 18 Oak Street
    AddressLine2 : Nyack
    AddressLine3 : NY
    PostalCode : 20342

    Ultimately, I want to convertto-CSV to have this as a CSV File which I can then import into my UPS shipping program.

    I tried various ForEach-Object.... but don't know if you can actually nest those? So, if an object contains strings fields and fields that are hash tables.... how would you print out the string field and the hash table field in a coherent record, where

    PS>$customer= @{Name="Davy Jones"; Delivery=@{Company="BigCorp"; Address="123 Corporate Lane"; City="Gotham"; State="NY"; Zip="03232"}}

    PS>
    Name Value
    —- —–
    Delivery {Zip, State, Company, Address...}
    Name Davy Jones

    I'm missing something here...

    Thanks.

  • #23285
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Try using Format-Custom. There's a parameter to control the enumeration depth.

    And yes, you can have nested ForEach loops, and nested ForEach-Object, although with the cmdlet it can get tough to keep track of what $_ refers to.

  • #23287
    Profile photo of Lawrence Keyes
    Lawrence Keyes
    Participant

    Don...thanks much! I'lll look in to Forma-Custom... hadn't thought of that.

    Could you provide a pointer to the syntax for the nested ForEach or ForEach-Object loops?
    I tried them both every which way, but can't seem to figure out how they would work.

    So, for my example
    $customer= @{Name="Davy Jones"; Delivery=@{Company="BigCorp"; Address="123 Corporate Lane"; City="Gotham"; State="NY"; Zip="03232"}}
    I would think it should be something like:

    $customer | foreach-object ($_) { (Out-String -InputObject $_.name) + (foreach-object -InputObject $_.delivery ($_) {Out-String – InputObject $_.Company})}

    Everything up to the plus sign works, but then I get errors;
    "You must provide a value folllowing the '+' operator"
    "Systems.collections.hashtable cannot be resolved to a method."

  • #23288
    Profile photo of Lawrence Keyes
    Lawrence Keyes
    Participant

    So, I eliminated the plus sign, clearly that wasn't working. I can get it to print out (at least) with this:

    $customer | ForEach-Object ($_) {
    Out-String -InputObject ( `
    $_.Name,  `
    $_.Delivery.Address,
    $_.Delivery.City , 
    $_.Delivery.State, 
    $_.Delivery.Zip)
    }
    

    Jones
    123 Corporate Lane
    Gotham
    NY
    03232

    piping this to get-member yields a string type.
    What I'd like is the name pairs.... which would then go nicely into convertto-CSV.

You must be logged in to reply to this topic.