PowerShell ConvertFrom-JSON

PowerShell ConvertFrom-JSON

JSON is the JavaScript Object Notation.  PowerShell version 3.0 introduced a special cmdlet to handle text formatted for JavaScript.

Topics for PowerShell's ConvertFrom-JSON

 ♣

Introduction to ConvertFrom-JSON

JSON (JavaScript Object Notation) is designed for data exchange; one use of JSON is as an alternative to XML.  Although it uses the same syntax as JavaScript, JSON is platform and language independent making it useful for web applications.

Key PowerShell Concept: Here-String @"..."@

PowerShell has a technique called Here-String for dealing with text; you need special care when handling text contain a lot of speech marks and other delimiters.  Languages such as VBScript handle speech marks with escape characters, however, I find the resulting syntax cumbersome, and my first attempts usually contain hard-to-find errors.  PowerShell handles this text data conversion easily, provided you master the Here-String construction. Here is a simple example:

@"
{"Stuff":["One":"Blue"]}
"@

Key point: Place @", and especially the final, "@ on their own line.
To be precise, the last thing on the first line must be @"
"@ must be the first thing on the last line.

PowerShell JSON Example

Clear-Host
$Input = @"
{
"Contacts": [
{ "FirstName":"John" , "LastName":"Evans" , "Email":"john.evans@cp.com" },
{ "FirstName":"Mira" , "LastName":"Cody" , "Email":"mira.cody@cp.com" },
{ "FirstName":"Trevor" , "LastName":"Williams", "Email":"trevor.williams@cp.com" }
      ]
}

"@

$Victim = $Input | ConvertFrom-JSON
$Victim.Contacts | Format-Table FirstName, LastName, Email -AutoSize

Note 1: Observe the crucial @".."@ wrapper

Note 2: Observe how PowerShell deals with the tricky syntax of the JavaScript "Contacts" above.  Check the punctuation of not only the speech marks, but also the colon, commas an two types of bracket.  Thank you ConvertFrom-JSON for dealing with all this syntax.

# ConvertFrom-JSON output result:

FirstName LastName Email 
--------- -------- -----
John Evans john.evans@cp.com
Mira Cody mira.cody@cp.com
Trevor Williams trevor.williams@cp.com

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

More Information About ConvertFrom-JSON

How did I know that: "The ConvertFrom-JSON cmdlet converts a JSON-formatted string to custom object (PSCustomObject) that has a property for each field in the JSON string"?  The answer is that I interrogated the cmdlet with PowerShell's Get-Help thus:

Clear-Host
Get-Help ConvertFrom-JSON -Full

Note 3: As is usual with Get-Help, the -Full parameter reveals examples of the cmdlet in action. 

Remember the Difference Between @" and @'.

Be aware of the significance of @" and @'.  PowerShell consistently treats the single quote as meaning literal, don't convert, whereas double quotes cause PowerShell to process variables as necessary.  Here is a suitable vehicle to see the difference:

# Problem: single quote.
Clear-host
$Math = @'
One is "$(1*1)"
Two is '$(1+1)'
Three is $(6/2)
'@
$Math

One is "$(1*1)"
Two is '$(1+1)'
Three is $(6/2)

Swap @" and "@ for @' and '@

## Result: Variable substitution inside @"...."@
One is "1"
Two is '2'
Three is 3

Troubleshooting ConvertFrom-JSON

Pay attention to sequencing.  Start with @" (and not "@); finish with "@ (and not @").

Employ the correct type of quotes.  Make sure that the text block itself employs only straight single or double quotes, not the curly variants.  These alternatives, which I don't advise, precede quotation mark with the word left or right: For example &rsquo or Alt+0146; (right single quotation mark ’ ).

Research More ConvertFrom Cmdlets

Clear-Host
Get-Command -verb ConvertFrom

Results:

Name
-----------------------
ConvertFrom-Csv
ConvertFrom-JSON
ConvertFrom-SecureString
ConvertFrom-StringData

The last cmdlet ConvertFrom-StringData surprised me; further research revealed that it also uses the here-string construction @'...'@.

See also ConvertFrom-Csv »

Summary of PowerShell's ConvertFrom-JSON Cmdlet

If you need to convert JavaScript syntax to PowerShell then version 3.0 brings ConvertTo-JSON and ConvertFrom-JSON cmdlets to handle the tricky exchange of formatted text.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-File   • Out-GridView   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: