1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShell : PSObject を ConvertTo-Json した際の Object 順を制御する。

Last updated at Posted at 2019-02-17

psobject を ConvertTo-Json で JSON 化した際に object の並びを書いた通りに出来るかという話。

Sample1.ps1
$ob = New-Object -TypeName psobject -Property @{
  FOO = 0
  BAR = 0
  BAZ = 0
  PIYO = 0
}
ConvertTo-Json $ob


{
 "BAZ":  0,
 "BAR":  0,
 "FOO":  0,
 "PIYO":  0
}
-> 順序が変わっている。
Sample2.ps1
$ob = New-Object -TypeName psobject -Property @{}
@('FOO', 'BAR', 'BAZ', 'PIYO') | % {
  $ob | Add-Member -MemberType NoteProperty -Name $_ -Value 0
} 
ConvertTo-Json $ob

{
    "FOO":  0,
    "BAR":  0,
    "BAZ":  0,
    "PIYO":  0
}
-> 指定した順序になっている。
1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?