다음을 통해 공유


ConvertFrom-Json

Converts a JSON-formatted string to a custom object.

구문

Default (기본값)

ConvertFrom-Json
    [-InputObject] <String>
    [<CommonParameters>]

Description

The ConvertFrom-Json cmdlet 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 web sites 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. See other examples below.

To generate a JSON string from any object, use the ConvertTo-Json cmdlet.

This cmdlet was introduced in PowerShell 3.0.

Note

In Windows PowerShell 5.1, ConvertFrom-Json returns an error when it encounters a JSON comment. In PowerShell 6 and higher, the cmdlet supports JSON with comments. JSON comments aren't captured in the objects output by the cmdlet. For more information, see the JSON comments section of the about_Comments article.

예제

Example 1: Convert a DateTime object to a JSON object

This command uses the ConvertTo-Json and ConvertFrom-Json cmdlets to convert a DateTime object from the Get-Date cmdlet to a JSON object then to a PSCustomObject.

Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json
DisplayHint : 2
DateTime    : Friday, January 13, 2012 8:06:31 PM
Date        : 1/13/2012 8:00:00 AM
Day         : 13
DayOfWeek   : 5
DayOfYear   : 13
Hour        : 20
Kind        : 2
Millisecond : 400
Minute      : 6
Month       : 1
Second      : 31
Ticks       : 634620819914009002
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}
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.

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.

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

Example 3: Convert a JSON string to a custom object

This example shows how to use the ConvertFrom-Json cmdlet to convert a JSON file to a PowerShell custom object.

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

The command uses Get-Content cmdlet to get the strings in a JSON file. The Raw parameter returns the whole file as a single JSON object. Then it uses the pipeline operator to send the delimited string to the ConvertFrom-Json cmdlet, which converts it to a custom object.

매개 변수

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

The InputObject parameter is required, but its value can be an empty string. When the input object is an empty string, ConvertFrom-Json doesn't generate any output. The InputObject value can't be $null.

매개 변수 속성

형식:String
Default value:None
와일드카드 지원:False
DontShow:False

매개 변수 집합

(All)
Position:0
필수:True
파이프라인의 값:True
속성 이름별 파이프라인의 값:False
나머지 인수의 값:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

입력

String

You can pipe a JSON string to ConvertFrom-Json.

출력

PSCustomObject

참고

The ConvertFrom-Json cmdlet is implemented using the JavaScriptSerializer class.

The PSObject type maintains the order of the properties as presented in the JSON string. While the key-value pairs are added to the Hashtable in the order presented in the JSON string, Hashtable objects don't maintain that order.