Skip to content

Instantly share code, notes, and snippets.

@poshcodebear
Last active August 29, 2015 14:26
Show Gist options
  • Save poshcodebear/840658e88c8f3aaadd5d to your computer and use it in GitHub Desktop.
Save poshcodebear/840658e88c8f3aaadd5d to your computer and use it in GitHub Desktop.
Scripting Games August 2015 entry
# Works with PowerShell v2, but only works with very simple JSON
function Get-SimpleJSON {
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$True)]
[Alias('Uri')]
[string]$JSONUri,
[Alias('CamelCase')]
[switch]$TitleCase
)
if ($JSONUri -notlike "*://*") { $JSONUri = "http://$($JSONUri)" }
# Create web client and request the JSON output
$webClient = New-Object -TypeName System.Net.WebClient
$rawOutput = $webClient.DownloadString($JSONUri)
# Trim the JSON and make it easier for me to parse
$rawObject = $rawOutput.trim().trim('{','}').replace(',"',';').replace('":','=').replace('"','').split(';')
# Dynamically build output object properties hashtable
$props = @{}
foreach ($prop in $rawObject) {
# Split the key=value pair
$key = $prop.Split('=')[0]
$value = $prop.Split('=')[1]
if ($TitleCase) {
# Convert key name to Title (or Camel) case
$key = (Get-Culture).TextInfo.ToTitleCase($key.ToLower())
}
# Add the current property to the hashtable
$props.Add($key, $value)
}
# Create the object and write it out
$obj = New-Object -TypeName PSObject -Property $props
Write-Output -InputObject $obj
}
# And here's it in use:
Get-SimpleJSON -JSONUri www.telize.com/geoip | Select-Object longitude, latitude, continent_code, country_code3 | Format-Table -AutoSize
# Or as a one-liner for PowerShell v3+:
ConvertFrom-Json -InputObject (Invoke-WebRequest -Uri www.telize.com/geoip).Content | Select-Object longitude, latitude, continent_code, country_code3 | Format-Table -AutoSize
@poshcodebear
Copy link
Author

Some thoughts behind this:

After playing with this puzzle for a little bit, I realized I could easily solve the basic puzzle with a call to Invoke-WebRequest and passing the "Content" property of that on to ConvertFrom-JSON. That seemed too easy, but then I realized that neither of those cmdlets exist in PowerShell version 2! So, I decided it would be a more interesting challenge to solve the puzzle for PSv2, and make it a functional cmdlet while I'm at it.

So, to start, I created a web client using the .NET class System.Net.WebClient, then used it to download the supplied URI. Then, I trimmed it and replaced certain specific characters that helped make it easier to work with for me, and split the output in to an array of key=value pairs.

Once I had that, I just dynamically built a hash table using those pairs, attached the hash table as a list of properties to a new object, and wrote the object out to the pipeline.

Note: this works with PowerShell version 2 and newer, but unlike ConvertFrom-JSON, it won't work with any complex JSON (basically, anything that is more complicated than a list of key=value pairs, such as if the value is an array or hash table). I tried to come up with a way to handle complex JSON, but it's a lot more work than I'm ready to put into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment