Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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=
like image 693
Shashi Ranjan Avatar asked Apr 28 '14 13:04

Shashi Ranjan


People also ask

Can I use JSON in PowerShell?

PowerShell uses the two cmdlets ConvertTo-JSON and ConvertFrom-JSON to work with JSON files. The ConvertTo-JSON cmdlet converts any possible output to the JSON format and the ConvertFrom-JSON cmdlet converts the JSON input to the custom Object or the hashtable format.


1 Answers

You're question is very unclear. If you have Powershell 3 or later, you can use ConvertFrom-JSON to convert the JSON-response to a 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>"
like image 73
Frode F. Avatar answered Sep 28 '22 10:09

Frode F.