[英]Add new key value pair to JSON file in powershell.
我有一個包含以下內容的JSON文件:
{
"buildDate": "2017-08-16",
"version": "v1.2.0"
}
如何將新的鍵值對添加到現有JSON文件中? 例如,我想使用上面的JSON,並最終得到:
{
"buildDate": "2017-08-16",
"version": "v1.2.0",
"newKey1": "newValue1",
"newKey2": "newValue2"
}
我目前使用以下代碼寫入JSON:
@{buildDate="2017-08-16"; version="v1.2.0"} | ConvertTo-Json | Out-File .\data.json
將JSON數據轉換為PowerShell對象,添加新屬性,然后將該對象轉換回JSON:
$jsonfile = 'C:\path\to\your.json'
$json = Get-Content $jsonfile | Out-String | ConvertFrom-Json
$json | Add-Member -Type NoteProperty -Name 'newKey1' -Value 'newValue1'
$json | Add-Member -Type NoteProperty -Name 'newKey2' -Value 'newValue2'
$json | ConvertTo-Json | Set-Content $jsonfile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.