5
7

More than 3 years have passed since last update.

Powershellで色々とJSON形式に変換する

Posted at

Powershellで色々とJSON形式に変換する。

PowershellのConvertTo-JSONを使って何でもJSON形式にしてみる。

ソースコード
https://github.com/digitalMagicbox/ConvertToJSON

CSVファイル

1.CSVは、Import-CSVがあるのでとても簡単。

1行目をキーとして変換します。

CsvToJson
Import-CSV $csvfilename -Encoding Default | ConvertTo-Json | Out-File -FilePath $jsonfilename -Encoding UTF8 
入力:CSVファイル
Item1,Item2,Item3,Item4,Item5
1AAAA,1BBBB,1CCCC,1DDDD,1EEEE
2AAAA,2BBBB,2CCCC,2DDDD,2EEEE
3AAAA,3BBBB,3CCCC,3DDDD,3EEEE
出力:JSONファイル
[
  {
    "Item1": "1AAAA",
    "Item2": "1BBBB",
    "Item3": "1CCCC",
    "Item4": "1DDDD",
    "Item5": "1EEEE"
  },
  {
    "Item1": "2AAAA",
    "Item2": "2BBBB",
    "Item3": "2CCCC",
    "Item4": "2DDDD",
    "Item5": "2EEEE"
  },
  {
    "Item1": "3AAAA",
    "Item2": "3BBBB",
    "Item3": "3CCCC",
    "Item4": "3DDDD",
    "Item5": "3EEEE"
  }
]

レジストリファイル

1. Subkey内のキーをメンバーとしたオブジェクトを作成する。

  • 型は、とりあえずDWORD型、文字列型だけに対応。
  • 正規表現で文字列を分解する。
  • オブジェクトを作成する。
  • メンバーに値を格納する。
$obj = $l -match '^\"(?<Name>.*)\"=(?<Type>(\"|dword:))(?<Value>.*)$' | 
ForEach-Object { New-Object PSObject -Property $Matches } | 
Select-Object -Property Name, Type, Value

2. ルートキーは、HKLMなどの省略形に置換する。

# ルートキー置換用のリスト
$replacerootkeys = @{
    'HKLM' = 'HKEY_LOCAL_MACHINE';
    'HKCU' = 'HKEY_CURRENT_USER';
    'HKCR' = 'HKEY_CLASSES_ROOT';
    'HKU'  = 'HKEY_USERS';
    'HKCC' = 'HKEY_CURRENT_CONFIG';
}
# サブキーを囲んでいる”[”と”]”を消す。
$subkey = $l -replace '\[|\]', ''
# ルートキーを置換する。
foreach ($rootkey in $replacerootkeys.GetEnumerator()) {
    $subkey = $subkey -replace $rootkey.Value, $rootkey.Key
入力:レジストリ
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE¥¥Software¥¥SubKey1]
"Key1"=dword:00000001
"Key2"="Value2"
"Key3"=""

[HKEY_CURRENT_USER¥¥Software¥¥SubKey2]
"Key4"=dword:000000FF
"Key5"="ABCDEFG"
"Key6"="[]"

[HKEY_CURRENT_USER¥¥Software¥¥SubKey3]
出力:JSONファイル
[
  {
    "Subkey": "HKLM¥¥Software¥¥SubKey1",
    "Key1": 1,
    "Key2": "Value2",
    "Key3": ""
  },
  {
    "Subkey": "HKCU¥¥Software¥¥SubKey2",
    "Key4": 255,
    "Key5": "ABCDEFG",
    "Key6": "[]"
  }
]

INIファイル

1. セクションで1つのオブジェクトにする。

# セクションを囲んでいる”[”と”]”を消す。
$sec = $l -replace '\[|\]', ''
# 空のオブジェクトを作成
$keys = @{ }
# ConvertFrom-StringDataで変換するために、セクションをKey=Value形式にする。
$l = "Section=" + $sec

2. KeyName=Valueの形式は、「ConvertFrom-StringData」でオブジェクトに変換できる。

# ”+=”でオブジェクトに追加していく。
$keys += ConvertFrom-StringData($l)
入力:iniファイル
[Section1]
Key1=Value1
Key2=Value2

[Section2]
Key2-1=Value2-1
Key2-2 = Value2-2

[Section3]
Key3=

[SEction4]
出力:JSONファイル
[
  {
    "Section": "SEction4"
  },
  {
    "Key2": "Value2",
    "Section": "Section1",
    "Key1": "Value1"
  },
  {
    "Key2-1": "Value2-1",
    "Section": "Section2",
    "Key2-2": "Value2-2"
  },
  {
    "Key3": "",
    "Section": "Section3"
  }
]
5
7
0

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