Home > OS >  How to read hash table when you don't know title of column
How to read hash table when you don't know title of column

Time:08-03

$data hash table has the following

{
    "Aix_10pm":  {
                     "2022-07-23":  1.11,
                     "2022-07-24":  1.052,
                     "2022-07-27":  0.161,
                     "2022-07-31":  0.147,
                     "2022-07-26":  1.096,
                     "2022-07-21":  1.091,
                     "2022-07-30":  0.152,
                     "2022-07-25":  1.095,
                     "2022-07-29":  0.15,
                     "2022-07-22":  1.134,
                     "2022-07-19":  1.06,
                     "2022-07-28":  0.125,
                     "2022-08-01":  0.792,
                     "2022-07-20":  1.122
                 }
}

To output the above I wrote

$data.GetEnumerator()|ForEach-Object {"{0,-20} {1,10} {2,10} {3,10} {4,10} {5,10} {6,10}  {7,10} {8,10} {9,10} {10,10} {11,10} {12,10} {13,10}  {14,10}" -f $_.name,$_.value."2022-07-28",$_.value."2022-07-27",$_.value."2022-07-26",$_.value."2022-07-25",$_.value."2022-07-24",$_.value."2022-07-23",$_.value."2022-07-22",$_.value."2022-07-21",$_.value."2022-07-20",$_.value."2022-07-19",$_.value."2022-07-18",$_.value."2022-07-17",$_.value."2022-07-16",$_.value."2022-07-15"}

Is there better way to put output instead of manually typing dates?

CodePudding user response:

It looks like you want a tabular representation of the hashtable entries:

If you cast the (embedded) hashtable to [pscustomobject], you can pipe the result to Format-Table for a tabular display, with each entry becoming its own column:

[pscustomobject] $data.Aix_10pm | Format-Table

CodePudding user response:

When referring to a hashtable the "title" would be considered a key. When referring to an object, which is what you would end up with by default when using ConvertFrom-Json, the "title" would be considered a property. Going with your statement that $data is a hashtable and wanting to read it without knowing the key(s), you can iterate the keys to determine what keys are present

$data.keys

And you can retrieve the values with those keys.

$keylist = $data.keys

foreach($key in $keylist) {
    $data.$key
}
  • Related