1

I am trying to output to console a json object that is being returned when I call the URL:

http://localhost:999/api/randomObjects/19

where randomObjects is the type of object and 19 is the object ID. So far, the only way I have gotten the json to output to the console in powershell is using webclient and saving the json to a folder then outputting it.

$storageDir = "C:\Path\to\folder"
$webclient = New-Object System.Net.WebClient
$url = "http://localhost:999/api/randomObjects/19"
$file = "$storageDir/demofile.json"
$webclient.DownloadFile($url,$file)
cat $file

Is there a way to directly output the json object to console without saving it in a file?

SKLAK
  • 3,825
  • 9
  • 33
  • 57

2 Answers2

3

There sure is: See the ConvertTo-Json and ConvertFrom-Json cmdlets. These will transform to and from the JavaScript Object Notation textual format into a custom PSObject that you can query and manipulate using the regular tricks of the trade.

x0n
  • 51,312
  • 7
  • 89
  • 111
  • The prodigal son returns. ;-) – Keith Hill Jun 23 '14 at 17:59
  • Hah... yes, I know I've been absent for a few months now. It's also hard to beat MVP buggers like you to the punch :D – x0n Jun 23 '14 at 18:00
  • Thanks, this worked out great. For some reason it won't let me mark your answer as correct? Keeps saying "an error has occurred". I'll try again in a bit. – SKLAK Jun 23 '14 at 18:12
  • @SKLAK: You should now be able to mark this answer as the answer ;-) – Oliver Nov 09 '15 at 09:59
0

This may be a more direct method; if the API returns a JSON then it will convert to a PSObject if possible*.

$response = Invoke-RestMethod -Uri http://localhost:999/api/randomObjects/19

*if possible means "as long as the JSON isn't too big". See also:

Community
  • 1
  • 1
Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46