0

My aws cli returns multiline array with nested hash, I am not able to parse output to get appropriate values from it

>Write-Host $a
[
  [
     [
         {
             "VolumeID": "vol-fxxxxxx"
         }
     ]
   ]
]

If I do $a[0], it returns first line i.e. "[" same with incremental indexs. How I can parse this array and get the volumeID from it ? Thanks

Additional details :

Thanks for your valuable answers. Here is the class name of the above object :

> $a.getType()                                                                                                          
IsPublic IsSerial Name                                     BaseType                                                                                                             
 -------- -------- ----                                     --------                                                                                                             
True     True     Object[]                               System.Array

> $a | Where-Object ($_ -like "*Volume*")

> $a | ConvertFrom-Json
 ConvertFrom-Json : Invalid JSON primitive: 
 Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData.
 At line:1 char:6


 + $a | ConvertFrom-Json
 +      ~~~~~~~~~~~~~~~~
 + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
 + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

Hi Martin Brandl, TessellatingHeckler I have updated with your question, could you please help me out on the same. Thanks

2
  • can you post the output of $a.GetType() Commented Nov 21, 2016 at 6:19
  • Could it be that $a is just a multiline string, and $a[0] returns [ because that's the first character? Commented Nov 21, 2016 at 10:48

2 Answers 2

2

same with incremental indexs

I can't make sense of incremental indexes doing the same, and returning the first line, but it looks like it's either:

A multiline string ($a.gettype() has Name: String):

$b = $a | ConvertFrom-Json
$b[0][0][0].VolumeID

or, an array of string ($a.gettype() Name is Object[] or String[]), do this first, then convert from JSON:

$a = -join $a
Sign up to request clarification or add additional context in comments.

1 Comment

$b[0].VolumeID seems to suffice, despite the nested arrays.
0

You could use the where-object cmdlet.

$a | where-object {$_ -like "*Volume*"}

More information on the where-object cmdlet can be found here: https://technet.microsoft.com/en-us/library/ee177028.aspx

Thanks, Tim.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.