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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The prodigal son returns. ;-)
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
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: You should now be able to mark this answer as the answer ;-)
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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.