Convert a JSON-formatted string to a custom object.

      ConvertFrom-Json [-InputObject] String [CommonParameters]

   -InputObject String
        The JSON strings to convert to JSON objects. Enter a variable that contains the string, or type a 
        command or expression that gets the string. You can also pipe a string to ConvertFrom-Json .
        The -InputObject parameter is required, but its value can be an empty string.
        When the input object is an empty string, ConvertFrom-Json does not generate any output.

        The -InputObject value cannot be $Null.

Standard Aliases for ConvertFrom-Json: none, but if you want to add a short alias like cfjn, set it with set-alias

This cmdlet was introduced in Windows PowerShell 3.0.


Convert a DateTime object to a JSON object:

PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json

This command uses the Select-Object cmdlet to get all of the properties of the DateTime object. It uses ConvertTo-Json to convert the DateTime object to a JSON-formatted string and ConvertFrom-Json to convert the JSON-formatted string to a JSON object. The object properties: Date, Day, Hour etc are then displayed on screen.

Get JSON strings from a web service and convert them to Windows PowerShell objects:

# Ensures that Invoke-WebRequest uses TLS 1.2 
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$j = Invoke-WebRequest '' | ConvertFrom-Json

You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects.

Convert a JSON string to a hash table.

'{ "key":"value1", "Key":"value2" }' | ConvertFrom-Json -AsHashtable

The JSON string contains two key value pairs with keys that differ only in casing. Without the -AsHashtable switch, the command will throw an error.

“The price of anything is the amount of life you exchange for it” ~ Henry David Thoreau

Related PowerShell Cmdlets

ConvertTo-Json - Convert an object to a JSON-formatted string.
invoke-WebRequest - Get content from a web page on the Internet.
Invoke-RestMethod - Send an HTTP or HTTPS request to a RESTful web service.
How-To: Retrieve UK Bank Holidays using PowerShell

Copyright © 1999-2024
Some rights reserved