Skip to content

Commit

Permalink
Merge pull request #6773 from sdwheeler/sdw-i6597
Browse files Browse the repository at this point in the history
Fixes #6597 - add note about datetime conversion
  • Loading branch information
sdwheeler committed Oct 20, 2020
2 parents 138ec48 + 2f3e958 commit dd5e896
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
14 changes: 12 additions & 2 deletions reference/6/Microsoft.PowerShell.Utility/ConvertFrom-Json.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
keywords: powershell,cmdlet
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 08/17/2020
ms.date: 10/19/2020
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-6&WT.mc_id=ps-gethelp
schema: 2.0.0
title: ConvertFrom-Json
Expand Down Expand Up @@ -198,7 +198,17 @@ You can pipe a JSON string to `ConvertFrom-Json`.

## NOTES

The `ConvertFrom-Json` cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).
This cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).

Beginning in PowerShell 6, `ConvertTo-Json` attempts to convert strings formatted as timestamps to
**DateTime** values. The converted value is a `[datetime]` instance with a `Kind` property set as
follows:

- `Unspecified`, if there is no time zone information in the input string.
- `Utc`, if the time zone information is a trailing `Z`.
- `Local`, if the time zone information is given as a trailing UTC _offset_ like `+02:00`. The
offset is properly converted to the caller's configured time zone. The default output formatting
does not indicate the original time zone offset.

## RELATED LINKS

Expand Down
34 changes: 23 additions & 11 deletions reference/7.0/Microsoft.PowerShell.Utility/ConvertFrom-Json.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
keywords: powershell,cmdlet
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 08/17/2020
ms.date: 10/19/2020
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7&WT.mc_id=ps-gethelp
schema: 2.0.0
title: ConvertFrom-Json
Expand Down Expand Up @@ -67,24 +67,25 @@ TimeOfDay : @{Ticks=723914009002; Days=0; Hours=20; Milliseconds=400; Minutes=
Year : 2012
```

The example 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 string formatted as a
JSON object and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string to a
**PSCustomObject** object.
The example 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 string
formatted as a JSON object and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string
to a **PSCustomObject** object.

### Example 2: Get JSON strings from a web service and convert them to PowerShell objects

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.
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.

```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
```

You can also use the `Invoke-RestMethod` cmdlet, which automatically converts JSON content to objects.
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

Expand All @@ -101,7 +102,8 @@ custom object.

### Example 4: Convert a JSON string to a hash table

This command shows an example where the `-AsHashtable` switch can overcome limitations of the command.
This command shows an example where the `-AsHashtable` switch can overcome limitations of the
command.

```powershell
'{ "key":"value1", "Key":"value2" }' | ConvertFrom-Json -AsHashtable
Expand Down Expand Up @@ -234,7 +236,17 @@ You can pipe a JSON string to `ConvertFrom-Json`.

## NOTES

The `ConvertFrom-Json` cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).
This cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).

Beginning in PowerShell 6, `ConvertTo-Json` attempts to convert strings formatted as timestamps to
**DateTime** values. The converted value is a `[datetime]` instance with a `Kind` property set as
follows:

- `Unspecified`, if there is no time zone information in the input string.
- `Utc`, if the time zone information is a trailing `Z`.
- `Local`, if the time zone information is given as a trailing UTC _offset_ like `+02:00`. The
offset is properly converted to the caller's configured time zone. The default output formatting
does not indicate the original time zone offset.

## RELATED LINKS

Expand Down
28 changes: 19 additions & 9 deletions reference/7.1/Microsoft.PowerShell.Utility/ConvertFrom-Json.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
keywords: powershell,cmdlet
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 08/17/2020
ms.date: 10/19/2020
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: ConvertFrom-Json
Expand Down Expand Up @@ -67,16 +67,16 @@ TimeOfDay : @{Ticks=723914009002; Days=0; Hours=20; Milliseconds=400; Minutes=
Year : 2012
```

The example 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 string formatted as a
JSON object and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string to a
**PSCustomObject** object.
The example 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 string
formatted as a JSON object and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string
to a **PSCustomObject** object.

### Example 2: Get JSON strings from a web service and convert them to PowerShell objects

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.
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.

```powershell
# Ensures that Invoke-WebRequest uses TLS 1.2
Expand Down Expand Up @@ -236,7 +236,17 @@ You can pipe a JSON string to `ConvertFrom-Json`.

## NOTES

The `ConvertFrom-Json` cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).
This cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).

Beginning in PowerShell 6, `ConvertTo-Json` attempts to convert strings formatted as timestamps to
**DateTime** values. The converted value is a `[datetime]` instance with a `Kind` property set as
follows:

- `Unspecified`, if there is no time zone information in the input string.
- `Utc`, if the time zone information is a trailing `Z`.
- `Local`, if the time zone information is given as a trailing UTC _offset_ like `+02:00`. The
offset is properly converted to the caller's configured time zone. The default output formatting
does not indicate the original time zone offset.

## RELATED LINKS

Expand Down

0 comments on commit dd5e896

Please sign in to comment.