JSON 파일을 단일 값으로 업데이트하려고합니다. 파일이 업데이트되었지만 원하지 않거나 필요없는 추가 데이터가 업데이트되었습니다.PowerShell로 JSON 파일 업데이트
{
"name": "perf-a",
"cft_file":"cft/cft.json",
"parameters": {
"AppEnvironmentType": "perf",
"Image": "16963e76"
},
"tags": {
"Owner": "[email protected]",
"CostCenter": "12345"
}
}
"16963e76"여기
$outPath = "C:\Program Files (x86)\Go Agent\pipelines\mypipeline\Application\Application\Id.txt"
$imageid = Get-Content $outPath
$filejson = Get-Content -Raw -Path $file.FullName -Encoding UTF8 | ConvertFrom-Json
$filejson.parameters.Image = $imageid
$filejson | ConvertTo-Json | Out-File $filepath -Encoding utf8 -Force
내가 내 JSON의 모습을 기대하는 것입니다
Id.txt 한 문자열을 포함 : 여기
은 PowerShell 스크립트에서 미리보기입니다다음은 JSON이 실제로 업데이트 된 모습입니다.
{
"name": "perf-a",
"cft_file": "cft/cft.json",
"parameters": {
"AppEnvironmentType": "perf",
"Image": {
"value": "16963e76",
"PSPath": "C:\\Program Files (x86)\\Go Agent\\pipelines\\mypipeline\\Application\\Application\\Id.txt",
"PSParentPath": "C:\\Program Files (x86)\\Go Agent\\pipelines\\mypipeline\\Application\\Application",
"PSChildName": "Id.txt",
"PSDrive": "C",
"PSProvider": "Microsoft.PowerShell.Core\\FileSystem",
"ReadCount": 1
}
},
"tags": {
"Owner": "[email protected]",
"CostCenter": "12345"
}
}
이 문제의 원인은 무엇이며 어떻게 멈추게합니까?
감사합니다,
론다
id.txt에서 문자열을 가져올 때 그렇습니다. powershell이 환경에서 여러 개의 noteproperties를 추가하는 경우 대개 중요하지 않으므로 무시할 수 있습니다. 그러나 ConvertTo-JSON은 개체에서 이러한 노트 속성을 추출하여 json에 삽입합니다. tostring 메서드를 사용하면 내 솔루션은 문자열을 다른 문자열로 변경하지만 이번에는 노트 속성이 없으므로 작동합니다. –