diff --git a/reference/3.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md b/reference/3.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md index 9aff8837745a..5a756463ab7f 100644 --- a/reference/3.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md +++ b/reference/3.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md @@ -7,10 +7,12 @@ online version: http://go.microsoft.com/fwlink/?LinkID=217031 external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml title: ConvertFrom-Json --- - # ConvertFrom-Json + ## SYNOPSIS + Converts a JSON-formatted string to a custom object. + ## SYNTAX ``` @@ -18,15 +20,18 @@ ConvertFrom-Json [-InputObject] [] ``` ## DESCRIPTION + The **ConvertFrom-Json** cmdlet converts a JSON-formatted string to a custom object (PSCustomObject) that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects. To generate a JSON string from any object, use the **ConvertTo-Json** cmdlet. -This cmdlet is introduced in Windows PowerShell 3.0. +This cmdlet is introduced in PowerShell 3.0. + ## EXAMPLES ### Example 1 + ``` PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json @@ -65,29 +70,37 @@ This command uses the ConvertTo-Json and **ConvertFrom-Json** cmdlets to convert The command uses the Select-Object cmdlet to get all of the properties of the **DateTime** object. It uses the **ConvertTo-Json** cmdlet to convert the **DateTime** object to a JSON-formatted string and the **ConvertFrom-Json** cmdlet to convert the JSON-formatted string to a JSON object.. -### Example 2 -``` -PS C:\> $j = Invoke-WebRequest -Uri http://search.twitter.com/search.json?q=PowerShell | ConvertFrom-Json + +### Example 2: Get JSON strings from a web service and convert them to PowerShell objects + +```powershell +# 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 ``` -This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in Windows PowerShell. +This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in PowerShell. You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects. -### Example 3 -``` -PS C:\> (Get-Content JsonFile.JSON) -join "`n" | ConvertFrom-Json + +### Example 3: Convert a JSON string to a custom object + +```powershell +(Get-Content JsonFile.JSON) -join '`n' | ConvertFrom-Json ``` -This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a Windows PowerShell custom object. +This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a PowerShell custom object. The command uses Get-Content cmdlet to get the strings in a JSON file. It uses the Join operator to join the strings in the file into a single string that is delimited by newline characters (\`n). Then it uses the pipeline operator to send the delimited string to the **ConvertFrom-Json** cmdlet, which converts it to a custom object. The Join operator is required, because the **ConvertFrom-Json** cmdlet expects a single string. + ## PARAMETERS ### -InputObject + Specifies 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**. @@ -109,17 +122,22 @@ Accept wildcard characters: False ``` ### CommonParameters + This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). + ## INPUTS ### System.String + You can pipe a JSON string to **ConvertFrom-Json**. + ## OUTPUTS ### PSCustomObject ## NOTES -* The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). + +- The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). ## RELATED LINKS diff --git a/reference/4.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md b/reference/4.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md index ec7acd7a8b9e..c8b894a8ceb0 100644 --- a/reference/4.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md +++ b/reference/4.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md @@ -7,10 +7,10 @@ online version: http://go.microsoft.com/fwlink/p/?linkid=293947 external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml title: ConvertFrom-Json --- - # ConvertFrom-Json ## SYNOPSIS + Converts a JSON-formatted string to a custom object. ## SYNTAX @@ -20,16 +20,18 @@ ConvertFrom-Json [-InputObject] [] ``` ## DESCRIPTION + The **ConvertFrom-Json** cmdlet converts a JSON-formatted string to a custom object (PSCustomObject) that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects. To generate a JSON string from any object, use the **ConvertTo-Json** cmdlet. -This cmdlet is introduced in Windows PowerShell 3.0. +This cmdlet is introduced in PowerShell 3.0. ## EXAMPLES ### Example 1 + ``` PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json @@ -69,21 +71,25 @@ This command uses the ConvertTo-Json and **ConvertFrom-Json** cmdlets to convert The command uses the Select-Object cmdlet to get all of the properties of the **DateTime** object. It uses the **ConvertTo-Json** cmdlet to convert the **DateTime** object to a JSON-formatted string and the **ConvertFrom-Json** cmdlet to convert the JSON-formatted string to a JSON object.. -### Example 2 -``` -PS C:\> $j = Invoke-WebRequest -Uri http://search.twitter.com/search.json?q=PowerShell | ConvertFrom-Json +### Example 2: Get JSON strings from a web service and convert them to PowerShell objects + +```powershell +# 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 ``` -This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in Windows PowerShell. +This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in PowerShell. You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects. ### Example 3 -``` -PS C:\> (Get-Content JsonFile.JSON) -join "`n" | ConvertFrom-Json + +```powershell +(Get-Content JsonFile.JSON) -join '`n' | ConvertFrom-Json ``` -This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a Windows PowerShell custom object. +This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a PowerShell custom object. The command uses Get-Content cmdlet to get the strings in a JSON file. It uses the Join operator to join the strings in the file into a single string that is delimited by newline characters (\`n). @@ -94,6 +100,7 @@ The Join operator is required, because the **ConvertFrom-Json** cmdlet expects a ## PARAMETERS ### -InputObject + Specifies 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**. @@ -115,11 +122,13 @@ Accept wildcard characters: False ``` ### CommonParameters + This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). ## INPUTS ### System.String + You can pipe a JSON string to **ConvertFrom-Json**. ## OUTPUTS @@ -127,7 +136,8 @@ You can pipe a JSON string to **ConvertFrom-Json**. ### PSCustomObject ## NOTES -* The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). + +- The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). ## RELATED LINKS diff --git a/reference/5.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md b/reference/5.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md index 7030180db7e0..22a5bf906f97 100644 --- a/reference/5.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md +++ b/reference/5.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md @@ -7,10 +7,10 @@ online version: http://go.microsoft.com/fwlink/?LinkId=821753 external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml title: ConvertFrom-Json --- - # ConvertFrom-Json ## SYNOPSIS + Converts a JSON-formatted string to a custom object. ## SYNTAX @@ -20,16 +20,18 @@ ConvertFrom-Json [-InputObject] [] ``` ## DESCRIPTION + The **ConvertFrom-Json** cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom **PSCustomObject** object that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects. To generate a JSON string from any object, use the ConvertTo-Json cmdlet. -This cmdlet was introduced in Windows PowerShell 3.0. +This cmdlet was introduced in PowerShell 3.0. ## EXAMPLES ### Example 1: Convert a DateTime object to a JSON object + ``` PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json @@ -69,21 +71,25 @@ This command uses the ConvertTo-Json and **ConvertFrom-Json** cmdlets to convert The command uses the Select-Object cmdlet to get all of the properties of the **DateTime** object. It uses the **ConvertTo-Json** cmdlet to convert the **DateTime** object to a JSON-formatted string and the **ConvertFrom-Json** cmdlet to convert the JSON-formatted string to a JSON object.. -### Example 2: Get JSON strings from a web service and convert them to Windows PowerShell objects -``` -PS C:\> $J = Invoke-WebRequest -Uri http://search.twitter.com/search.json?q=PowerShell | ConvertFrom-Json +### Example 2: Get JSON strings from a web service and convert them to PowerShell objects + +```powershell +# 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 ``` -This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in Windows PowerShell. +This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in PowerShell. You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects. ### Example 3: Convert a JSON string to a custom object -``` -PS C:\> (Get-Content JsonFile.JSON) -Join "`n" | ConvertFrom-Json + +```powershell +(Get-Content JsonFile.JSON) -Join '`n' | ConvertFrom-Json ``` -This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a Windows PowerShell custom object. +This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a PowerShell custom object. The command uses Get-Content cmdlet to get the strings in a JSON file. It uses the Join operator to join the strings in the file into a single string that is delimited by newline characters (\`n). @@ -94,6 +100,7 @@ The Join operator is required, because the **ConvertFrom-Json** cmdlet expects a ## PARAMETERS ### -InputObject + Specifies 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**. @@ -115,11 +122,13 @@ Accept wildcard characters: False ``` ### CommonParameters + This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). ## INPUTS ### System.String + You can pipe a JSON string to **ConvertFrom-Json**. ## OUTPUTS @@ -127,7 +136,8 @@ You can pipe a JSON string to **ConvertFrom-Json**. ### PSCustomObject ## NOTES -* The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). + +- The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). ## RELATED LINKS diff --git a/reference/5.1/Microsoft.PowerShell.Utility/ConvertFrom-Json.md b/reference/5.1/Microsoft.PowerShell.Utility/ConvertFrom-Json.md index fc28ab77f7a4..842611dda337 100644 --- a/reference/5.1/Microsoft.PowerShell.Utility/ConvertFrom-Json.md +++ b/reference/5.1/Microsoft.PowerShell.Utility/ConvertFrom-Json.md @@ -7,10 +7,10 @@ online version: http://go.microsoft.com/fwlink/?LinkId=821753 external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml title: ConvertFrom-Json --- - # ConvertFrom-Json ## SYNOPSIS + Converts a JSON-formatted string to a custom object. ## SYNTAX @@ -20,16 +20,18 @@ ConvertFrom-Json [-InputObject] [] ``` ## DESCRIPTION + The **ConvertFrom-Json** cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom **PSCustomObject** object that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects. To generate a JSON string from any object, use the ConvertTo-Json cmdlet. -This cmdlet was introduced in Windows PowerShell 3.0. +This cmdlet was introduced in PowerShell 3.0. ## EXAMPLES ### Example 1: Convert a DateTime object to a JSON object + ``` PS C:\> Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json DisplayHint : 2 @@ -54,21 +56,25 @@ This command uses the ConvertTo-Json and **ConvertFrom-Json** cmdlets to convert The command uses the Select-Object cmdlet to get all of the properties of the **DateTime** object. It uses the **ConvertTo-Json** cmdlet to convert the **DateTime** object to a JSON-formatted string and the **ConvertFrom-Json** cmdlet to convert the JSON-formatted string to a JSON object. -### Example 2: Get JSON strings from a web service and convert them to Windows PowerShell objects -``` -PS C:\> $j = Invoke-WebRequest -Uri http://search.twitter.com/search.json?q=PowerShell | ConvertFrom-Json +### Example 2: Get JSON strings from a web service and convert them to PowerShell objects + +```powershell +# 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 ``` -This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in Windows PowerShell. +This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the **ConvertFrom-Json** cmdlet to convert JSON content to objects that can be managed in PowerShell. You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects. ### Example 3: Convert a JSON string to a custom object -``` -PS C:\> (Get-Content JsonFile.JSON) -join "`n" | ConvertFrom-Json + +```powershell +(Get-Content JsonFile.JSON) -join '`n' | ConvertFrom-Json ``` -This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a Windows PowerShell custom object. +This example shows how to use the **ConvertFrom-Json** cmdlet to convert a JSON file to a PowerShell custom object. The command uses Get-Content cmdlet to get the strings in a JSON file. It uses the Join operator to join the strings in the file into a single string that is delimited by newline characters (\`n). @@ -79,6 +85,7 @@ The Join operator is required, because the **ConvertFrom-Json** cmdlet expects a ## PARAMETERS ### -InputObject + Specifies 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**. @@ -100,11 +107,13 @@ Accept wildcard characters: False ``` ### CommonParameters + This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). ## INPUTS ### System.String + You can pipe a JSON string to **ConvertFrom-Json**. ## OUTPUTS @@ -112,7 +121,8 @@ You can pipe a JSON string to **ConvertFrom-Json**. ### PSCustomObject ## NOTES -* The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). + +- The **ConvertFrom-Json** cmdlet is implemented by using the [JavaScriptSerializer class](https://msdn.microsoft.com/library/system.web.script.serialization.javascriptserializer). ## RELATED LINKS diff --git a/reference/6/Microsoft.PowerShell.Utility/ConvertFrom-Json.md b/reference/6/Microsoft.PowerShell.Utility/ConvertFrom-Json.md index 9652e4e63076..b767e4472daf 100644 --- a/reference/6/Microsoft.PowerShell.Utility/ConvertFrom-Json.md +++ b/reference/6/Microsoft.PowerShell.Utility/ConvertFrom-Json.md @@ -7,7 +7,6 @@ online version: http://go.microsoft.com/fwlink/?LinkId=821753 external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml title: ConvertFrom-Json --- - # ConvertFrom-Json ## SYNOPSIS @@ -32,7 +31,7 @@ See other examples below. To generate a JSON string from any object, use the ConvertTo-Json cmdlet. -This cmdlet was introduced in Windows PowerShell 3.0. +This cmdlet was introduced in PowerShell 3.0. ## EXAMPLES @@ -64,22 +63,24 @@ The command uses the Select-Object cmdlet to get all of the properties of the ** It uses the `ConvertTo-Json` cmdlet to convert the **DateTime** object to a JSON-formatted string and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string to a JSON object. -### Example 2: Get JSON strings from a web service and convert them to Windows PowerShell objects +### Example 2: Get JSON strings from a web service and convert them to PowerShell objects ```powershell -PS C:\> $j = Invoke-WebRequest -Uri http://search.twitter.com/search.json?q=PowerShell | ConvertFrom-Json +# 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 ``` This command uses the `Invoke-WebRequest` cmdlet to get JSON strings from a web service and then it uses the `ConvertFrom-Json` cmdlet to convert JSON content to objects -that can be managed in Windows PowerShell. +that can be managed in PowerShell. You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects. ### Example 3: Convert a JSON string to a custom object ```powershell -PS C:\> (Get-Content JsonFile.JSON) -join "`n" | ConvertFrom-Json +(Get-Content JsonFile.JSON) -join '`n' | ConvertFrom-Json ``` This example shows how to use the `ConvertFrom-Json` cmdlet to convert a JSON file to a PowerShell custom object. @@ -95,7 +96,7 @@ The Join operator is required, because the `ConvertFrom-Json` cmdlet expects a s ### Example 4: Convert a JSON string to a hash table ```powershell -PS C:\> '{ "key":"value1", "Key":"value2" }' | ConvertFrom-Json -AsHashtable +'{ "key":"value1", "Key":"value2" }' | ConvertFrom-Json -AsHashtable ``` This command shows an example where the `-AsHashtable` switch can overcome limitations of the command. @@ -127,6 +128,7 @@ Accept wildcard characters: False ``` ### -AsHashtable + Converts the JSON to a hash table object. This switch was introduced in PowerShell 6.0. There are several scenarios where it can overcome some limitations of the `ConvertFrom-Json` cmdlet. @@ -148,6 +150,7 @@ Accept wildcard characters: False ``` ### CommonParameters + This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). @@ -155,6 +158,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## INPUTS ### System.String + You can pipe a JSON string to `ConvertFrom-Json`. ## OUTPUTS