Skip to content

Commit 3b28000

Browse files
jborean93sdwheeler
andauthored
ConvertFrom-Json: Add -DateKind parameter (#10737)
* ConvertFrom-Json: Add -DateKind parameter Documents the -DateKind parameter that is part of the PR PowerShell/PowerShell#20925. * Fix formatting, style, and wording --------- Co-authored-by: Sean Wheeler <sean.wheeler@microsoft.com>
1 parent 7d74fa1 commit 3b28000

File tree

1 file changed

+85
-32
lines changed

1 file changed

+85
-32
lines changed

reference/7.5/Microsoft.PowerShell.Utility/ConvertFrom-Json.md

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 11/29/2023
5+
ms.date: 01/29/2024
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.5&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: ConvertFrom-Json
@@ -16,7 +16,8 @@ Converts a JSON-formatted string to a custom object or a hash table.
1616
## SYNTAX
1717

1818
```
19-
ConvertFrom-Json [-InputObject] <String> [-AsHashtable] [-Depth <Int32>] [-NoEnumerate] [<CommonParameters>]
19+
ConvertFrom-Json [-InputObject] <String> [-AsHashtable] [-DateKind <JsonDateKind>] [-Depth <Int32>]
20+
[-NoEnumerate] [<CommonParameters>]
2021
```
2122

2223
## DESCRIPTION
@@ -53,32 +54,38 @@ Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json
5354

5455
```Output
5556
DisplayHint : 2
56-
DateTime : Friday, January 13, 2012 8:06:31 PM
57-
Date : 1/13/2012 8:00:00 AM
58-
Day : 13
59-
DayOfWeek : 5
60-
DayOfYear : 13
61-
Hour : 20
57+
DateTime : Monday, January 29, 2024 3:10:26 PM
58+
Date : 1/29/2024 12:00:00 AM
59+
Day : 29
60+
DayOfWeek : 1
61+
DayOfYear : 29
62+
Hour : 15
6263
Kind : 2
63-
Millisecond : 400
64-
Minute : 6
64+
Millisecond : 931
65+
Microsecond : 47
66+
Nanosecond : 600
67+
Minute : 10
6568
Month : 1
66-
Second : 31
67-
Ticks : 634620819914009002
68-
TimeOfDay : @{Ticks=723914009002; Days=0; Hours=20; Milliseconds=400; Minutes=6; Seconds=31; TotalDays=0.83786343634490734; TotalHours=20.108722472277776; TotalMilliseconds=72391400.900200009; TotalMinutes=1206.5233483366667;TotalSeconds=72391.4009002}
69-
Year : 2012
69+
Second : 26
70+
Ticks : 638421378269310476
71+
TimeOfDay : @{Ticks=546269310476; Days=0; Hours=15; Milliseconds=931; Microseconds=47;
72+
Nanoseconds=600; Minutes=10; Seconds=26; TotalDays=0.632256146384259;
73+
TotalHours=15.1741475132222; TotalMilliseconds=54626931.0476;
74+
TotalMicroseconds=54626931047.6; TotalNanoseconds=54626931047600;
75+
TotalMinutes=910.448850793333; TotalSeconds=54626.9310476}
76+
Year : 2024
7077
```
7178

72-
The example uses the `Select-Object` cmdlet to get all of the properties of the **DateTime**
73-
object. It uses the `ConvertTo-Json` cmdlet to convert the **DateTime** object to a string
74-
formatted as a JSON object and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string
75-
to a **PSCustomObject** object.
79+
The example uses the `Select-Object` cmdlet to get all of the properties of the **DateTime** object.
80+
It uses the `ConvertTo-Json` cmdlet to convert the **DateTime** object to a string formatted as a
81+
JSON object and the `ConvertFrom-Json` cmdlet to convert the JSON-formatted string to a
82+
**PSCustomObject** object.
7683

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

79-
This command uses the `Invoke-WebRequest` cmdlet to get JSON strings from a web service
80-
and then it uses the `ConvertFrom-Json` cmdlet to convert JSON content to objects
81-
that can be managed in PowerShell.
86+
This command uses the `Invoke-WebRequest` cmdlet to get JSON strings from a web service and then it
87+
uses the `ConvertFrom-Json` cmdlet to convert JSON content to objects that can be managed in
88+
PowerShell.
8289

8390
```powershell
8491
# Ensures that Invoke-WebRequest uses TLS 1.2
@@ -98,9 +105,9 @@ custom object.
98105
Get-Content -Raw JsonFile.JSON | ConvertFrom-Json
99106
```
100107

101-
The command uses Get-Content cmdlet to get the strings in a JSON file. The **Raw** parameter
102-
returns the whole file as a single JSON object. Then it uses the pipeline operator to send the
103-
delimited string to the `ConvertFrom-Json` cmdlet, which converts it to a custom object.
108+
The command uses Get-Content cmdlet to get the strings in a JSON file. The **Raw** parameter returns
109+
the whole file as a single JSON object. Then it uses the pipeline operator to send the delimited
110+
string to the `ConvertFrom-Json` cmdlet, which converts it to a custom object.
104111

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

@@ -163,6 +170,34 @@ Accept pipeline input: False
163170
Accept wildcard characters: False
164171
```
165172
173+
### -DateKind
174+
175+
Specifies the method used when parsing date time values in the JSON string. The acceptable values
176+
for this parameter are:
177+
178+
- `Default`
179+
- `Local`
180+
- `Utc`
181+
- `Offset`
182+
- `String`
183+
184+
For information about how these values affect conversion, see the details in the [NOTES](#notes).
185+
186+
This parameter was introduced in PowerShell 7.5.
187+
188+
```yaml
189+
Type: Microsoft.PowerShell.Commands.JsonDateKind
190+
Parameter Sets: (All)
191+
Aliases:
192+
Accepted values: Default, Local, Utc, Offset, String
193+
194+
Required: False
195+
Position: Named
196+
Default value: Default
197+
Accept pipeline input: False
198+
Accept wildcard characters: False
199+
```
200+
166201
### -Depth
167202

168203
Gets or sets the maximum depth the JSON input is allowed to have. The default is 1024.
@@ -246,14 +281,28 @@ You can pipe a JSON string to `ConvertFrom-Json`.
246281
This cmdlet is implemented using [Newtonsoft Json.NET](https://www.newtonsoft.com/json).
247282

248283
Beginning in PowerShell 6, `ConvertTo-Json` attempts to convert strings formatted as timestamps to
249-
**DateTime** values. The converted value is a `[datetime]` instance with a `Kind` property set as
250-
follows:
251-
252-
- `Unspecified`, if there is no time zone information in the input string.
253-
- `Utc`, if the time zone information is a trailing `Z`.
254-
- `Local`, if the time zone information is given as a trailing UTC _offset_ like `+02:00`. The
255-
offset is properly converted to the caller's configured time zone. The default output formatting
256-
doesn't indicate the original time zone offset.
284+
**DateTime** values.
285+
286+
PowerShell 7.5 added the **DateKind** parameter, which allows you to control how timestamp string
287+
are converted. The parameter accepts the following values:
288+
289+
- `Default` - Converts the timestamp to a `[datetime]` instance according to the following rules:
290+
- If there is no time zone information in the input string, the Json.NET serializer converts the
291+
value as an unspecified time value.
292+
- If the time zone information is a trailing `Z`, the Json.NET serializer converts the timestamp
293+
to a _UTC_ value.
294+
- If the timestamp includes a UTC offset like `+02:00`, the offset is converted to the caller's
295+
configured time zone. The default output formatting doesn't indicate the original time zone
296+
offset.
297+
- `Local` - Converts the timestamp to a `[datetime]` instance in the _local_ time. If the timestamp
298+
includes a UTC offset, the offset is converted to the caller's configured time zone. The default
299+
output formatting doesn't indicate the original time zone offset.
300+
- `Utc` - Converts the value to a `[datetime]` instance in UTC time.
301+
- `Offset` - Converts the timestamp to a `[DateTimeOffset]` instance with the timezone offset of the
302+
original string preserved in that instance. If the raw string did not contain a timezone offset,
303+
the **DateTimeOffset** value will be specified in the local timezone.
304+
- `String` - Preserves the value the `[string]` instance. This ensures that any custom parsing logic
305+
can be applied to the raw string value.
257306

258307
The **PSObject** type maintains the order of the properties as presented in the JSON string.
259308
Beginning with PowerShell 7.3, The **AsHashtable** parameter creates an **OrderedHashtable**. The
@@ -269,3 +318,7 @@ preserves that order.
269318
[Invoke-WebRequest](Invoke-WebRequest.md)
270319

271320
[Invoke-RestMethod](Invoke-RestMethod.md)
321+
322+
[DateTime](xref:System.DateTime)
323+
324+
[DateTimeOffset](xref:System.DateTimeOffset)

0 commit comments

Comments
 (0)