ConvertFrom-Json

Convert a JSON-formatted string to a custom object.

Syntax
      ConvertFrom-Json [-InputObject] String 
         [-AsHashtable] [-DateKind JsonDateKind] [-Depth Int32]
            [-NoEnumerate] [CommonParameters]
Key
   -AsHashtable
      Convert the JSON to a hash table object. PowerShell 6.0+.
      A blog post explaining the details of this when importing a JSON string.
      Starting with PowerShell 7.3, the object is an Ordered Hashtable and preserves the ordering of
      the keys from the JSON.

   -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 first introduced in Windows PowerShell 3.0 and revised in PowerShell 7.0.

ConvertFrom-Json converts a JavaScript Object Notation (JSON) formatted string to a custom PSObject or Hashtable object that has a property for each field in the JSON string.

JSON is commonly used by websites to provide a textual representation of objects. The cmdlet adds the properties to the new object as it processes each line of the JSON string. The JSON standard allows duplicate key names, which are prohibited in PSObject and Hashtable types. For example, if the JSON string contains duplicate keys, only the last key is used by this cmdlet.

Examples

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 'https://api.github.com/repos/PowerShell/PowerShell/issues' | 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.

Convert a JSON file to a PowerShell custom object:

Get-Content -Raw JsonFile.json | ConvertFrom-Json

Round-trip a single element json array:

Write-Output "With -NoEnumerate: $('[1]' | ConvertFrom-Json -NoEnumerate |
    ConvertTo-Json -Compress)"

Write-Output "Without -NoEnumerate: $('[1]' | ConvertFrom-Json |
    ConvertTo-Json -Compress)"

With -NoEnumerate: [1]
Without -NoEnumerate: 1

“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.
ConvertFrom-String - Extract and parse structured properties from a string.
ConvertFrom-CSV - Create objects from CSV variable-length strings that are generated by ConvertTo-CSV.
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-2026 SS64.com
Some rights reserved