I open sourced my PowerShell JSON parser

by Doug Finke on June 20, 2007

in JSON, PowerShell, Uncategorized

In previous posts I showed Convert-FromJSON as both a PowerShell script and C# implementation.

They are now hosted as open source in CodePlex. Also available is the Cmdlet source wrapping the C#, showing another integration path for PowerShell.

Douglas Crockford, author of JavaScript Object Notation, was kind enough to add a link to json.org. Thank you sir.

{ 4 comments… read them below or add one }

1 Grant 06.20.07 at 7:19 pm

got to love opensource – thanks Doug!!!!

2 Atif Aziz 06.22.07 at 10:25 am

Nice work, Douglas. In fact, this inspired me to add PowerShell scripting support to Jayrock (http://jayrock.berlios.de/), which is also an open source library that adds JSON support to .NET Framework in various forms. Here’s first using Jayrock from PowerShell to import JSON (nothing special here)…

PS C:\> [Reflection.Assembly]::LoadFile(’C:\Jayrock\bin\Jayrock.Json.dll’)

GAC Version Location
— ——- ——–
False v1.1.4322 C:\Jayrock\bin\Jayrock.Json.dll

PS C:\> [Jayrock.Json.Conversion.JsonConvert]::Import(’[{"title":"PowerShell"},{"title":"Test"}]‘)

Name Value
—- —–
title PowerShell
title Test

One of the things Jayrock allows you to do is to expose web services using JSON-RPC. The bit of twist I added now is to dynamically generate a PowerShell script from the server so you can call server-side methods and automate the service. Here’s how to download the PowerShell proxy (here pointing to a demo service running on my site):

PS C:\> $wc = New-Object Net.WebClient
PS C:\> $wc.DownloadString(’http://www.raboof.com/projects/jayrock/demo.ashx?ps1proxy’) | Invoke-Expression

Hey, this does not even require you to change the script execution policy. :) With this, you now get a function called DemoService (based on your service) that you can call to create an instance of the proxy:

PS C:\> $s = DemoService

Next, just start calling the methods of this object…

PS C:\> $s.now()
2007-06-22T09:21:03.8324368-06:00
PS C:\> [int] $s.sum(123, 456)
579

If you pipe $s to Get-Member, you’ll see how the proxy is setup.

Anyway, would appreciate your feedback, epsecially if you manage to find some time take a peek at the proxy code and offer some suggestions.

3 Doug Finke 06.22.07 at 4:41 pm

Excellent. Yes I played a bit with Jayrock.

I used it before I decided to write this parser.

Good stuff. I will take a look and let you know.

4 Rich 12.24.09 at 1:39 pm

Looks like this doesn’t handle escaped characters like \r\n or an escaped quotation like \”

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>