Using PowerShell v3 to consume the StackOverflow JSON API

by Doug Finke on February 8, 2012

in C#,JSON,Microsoft,PowerShell,PowerShell V3,StackOverflow,Windows 7,Windows 8

I was reading The Morning Brew and came across the entry below. I needed a break from writing my PowerShell Book, so I worked up the C# solution in PowerShell

Using to consume the JSON Stack Overflow API – Jonathan Creamer takes a look at consuming JSON powered APIs using .NET code, looking at the JSON.NET library as probably the best way of consuming such data, and exploring its use consuming the StackOverflow API.

PowerShell v3 Solution

It’s worth comparing the number of lines it takes to get this done in C# vs. PowerShell. Put another way, Ceremony vs. Essence. Plus, this script is a one to one mapping to the C# done by Jonathan. I’m seamlessly integrating PowerShell with the .NET Framework. The only difference is I use the PowerShell Cmdlet ConvertFrom-Json. That does all the heavy lifting, converting the JSON to PowerShell objects with properties. No need to download a 3rd party DLL like Check out the results below.

function Get-StackOverflowUser ($id="110865") {            
    $url = "$id"            
    $data = (New-Object Net.WebClient).DownloadData($url)            
    $memoryStream = New-Object System.IO.MemoryStream(,$data)            
    $decompress = New-Object `
    $reader = New-Object System.IO.StreamReader($decompress)            
    $ret = $reader.ReadToEnd()            
    ($ret | ConvertFrom-Json).Users            
Get-StackOverflowUser 110865


user_id             : 110865
user_type           : registered
creation_date       : 1242960794
display_name        : Doug Finke
reputation          : 1666
email_hash          : 94c48c63e7e63f5e713f7f7a5cdbcac0
last_access_date    : 1328745843
website_url         :
location            : New York, NY
about_me            : Software Developer and Microsoft PowerShell MVP
question_count      : 7
answer_count        : 72
view_count          : 70
up_vote_count       : 72
down_vote_count     : 2
accept_rate         : 71
user_questions_url  : /users/110865/questions
user_answers_url    : /users/110865/answers
user_favorites_url  : /users/110865/favorites
user_tags_url       : /users/110865/tags
user_badges_url     : /users/110865/badges
user_timeline_url   : /users/110865/timeline
user_mentioned_url  : /users/110865/mentioned
user_comments_url   : /users/110865/comments
user_reputation_url : /users/110865/reputation
badge_counts        : @{gold=0; silver=3; bronze=9}


If you’re running Windows 7, you can download PowerShell v3 CTP2 HERE. I hear the Windows 8 Consumer Preview could  launch February 29. Windows 8 comes with PowerShell v3 and both are pretty slick.

Well, back to writing another chapter in my PowerShell book, “Building GUIs in PowerShell”.

{ 2 comments… read them below or add one }

Jonathan Creamer 02.08.12 at 8:01 pm

Glad you read my article! I haven’t gotten to use PowerShell much before, so it’s cool to see a C# example converted into PS. Good stuff…

Boe Prox 02.08.12 at 8:07 pm

Great stuff as always, Doug! Even though I am not a developer, I am still looking forward to checking out your book when it comes out.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

You are forbidden!