Shashi Ranjan
Shashi Ranjan

Reputation: 1561

Reading json response in windows power shell

I am using power shell code as:

   $web_client = new-object system.net.webclient
    $build_info=web_client.DownloadString("http://<URL>")
    $suitevm_build_number=
    $suitevmCLN=
    $webapp_build=
    $stats_build=

Output in browser while hitting http:// is:

{"message":null,"changeset":"340718","branch":"main","product":"productname","buildNumber":"1775951","todaysDate":"28-4-2014"}

What should I write the power shell code to get:

  $suitevm_build_number=
    $suitevmCLN=
    $webapp_build=
    $stats_build=

Upvotes: 17

Views: 31988

Answers (1)

Frode F.
Frode F.

Reputation: 54851

Your question is very unclear. If you have Powershell 3 or later, you can use ConvertFrom-JSON to convert the JSON response to an object.

$build_info=$web_client.DownloadString("http://<URL>") | ConvertFrom-Json

Ex of output:

$build_info

message     : 
changeset   : 340718
branch      : main
product     : productname
buildNumber : 1775951
todaysDate  : 28-4-2014

With PS 3+ you could also replace the WebClient with Invoke-RestMethod as shown by @RickH.

$build_info = Invoke-RestMethod -Uri "http://<URL>"

Upvotes: 19

Related Questions