Sorting json response using powershell scripting -


i making rest api call , getting json response in return. want highest string response.

$x = invoke-restmethod -method -uri $r 

this returns

    {   "results": [     {       "uri": "h//foo/test.1.0.19.4.n"     },     {       "uri": "h://foo/test.1.0.20.6.n"     },     {       "uri": "h://foo/test.1.0.20.7.n"     }   ] } 

from want extract "test.1.0.20.7.n", largest when compared between .19 , .20 , when compare last 1 want .7 on .6

i have been trying use

$x| select uri |  sort-object uri  

it prints

test... test... test... 

you can leveraging "custom" sort expression blogged earlier. following sample works me:

$json = @"     {   "results": [     {       "uri": "h://foo/test.1.0.19.4.n"     },     {       "uri": "h://foo/test.1.0.20.7.n"     },     {       "uri": "h://foo/test.1.0.20.6.n"     }   ] } "@  $obj = convertfrom-json $json  $obj.results | sort-object -property @{expression={[int]($_.uri -replace "\d", "")}} | select -last 1 

output:

uri                     ---                     h://foo/test.1.0.20.7.n 

and dataset:

    {   "results": [     {       "uri": "h://foo/test.1.0.19.4.n"     },     {       "uri": "h://foo/test.1.1.5.7.n"     },     {       "uri": "h://foo/test.1.2.20.1.n"     },     {       "uri": "h://foo/test.1.0.20.7.n"     },     {       "uri": "h://foo/test.1.0.20.6.n"     }   ] } 

the result is:

uri                     ---                     h://foo/test.1.2.20.1.n 

Comments