Powershell을 사용하여 JSON 개체를 파일에 저장하는 방법
다음 JSON 파일을 powershell 표현 객체로 변환했습니다.
{
"computer": [
{
"children": [
{
"children": [ {
"children": [ {
"path": "T:\Dropbox\kvaki.html",
"name": "kvaki",
"type": "url",
"url": "http://example.com"
} ],
"path": "T:\Dropbox\",
"name": "Njusha",
"type": "folder"
}, {
"path": "T:\Dropbox\Europa.html",
"name": "Europa",
"type": "url",
"url": "http://example.com"
}, {
"path": "T:\Dropbox\math.html",
"name": "math",
"type": "url",
"url": "http://example.com"
} ],
"path": "T:\Dropbox\",
"name": "Money",
"type": "folder"
}
],
"full_path_on_file_sys": "T:\Dropbox\"
}
]
}
powershell 표현으로 몇 가지 연산을 한 후 JSON으로 파일에 저장하고 싶습니다.하지만 명령어$jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json"
이 방법으로 절약하다
{
"computer": [
{
"children": " ",
"full_path_on_file_sys": "T:\Dropbox\"
}
]
}
질문: 복잡한 파워셸 JSON 표현을 올바르게 저장하려면 어떻게 해야 합니까?
ConvertTo-Json에 대한 깊은 인수로 문제를 해결합니다.
$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
Set-Content 또는 Out-File로 파이프하기만 하면 됩니다.
Get-Process powershell |
ConvertTo-Json |
Set-Content json.txt
출력을 보고 파일에 저장하려면 tee 명령을 파이프에 연결할 수 있습니다.
Get-Process powershell | ConvertTo-Json | Tee-Object json.txt
$json.properties.metadata | ConvertTo-Json -Compress
PowerShell Version 2를 사용하는 경우 'PowerShell Code Repository'의 Joel Bennett의 JSON 모듈이 도움이 될 수 있습니다.
1) 다음 명령을 사용하여 json을 CSV로 변환할 수 있습니다.
예:Get-Content package.json | Out-String | ConvertFrom-Json | Select parameter1, parameter2, parameter3 | ConvertTo-Csv -NoTypeInformation | Format-Table >> C:\JenkinsWorkspace\Result.csv
콘텐츠 가져오기:이는 Linux의 "cat" 명령어와 같습니다.이 명령어는 파일 "package.json"의 모든 데이터를 가져와 Json에서 변환합니다(Convert From-Json 함수 사용). 필요한 파라미터만 추출한 후 불필요한 타입 헤더를 사용하지 않고 "Convert To-Csv" 함수를 사용하여 CSV로 변환하여 테이블 형식으로 만듭니다.
2) 위의 결과를 적절한 csv로 포맷하여 중복되지 않고 Excel 형식으로 표시할 수 있으며, 아래 명령어를 사용하여 텍스트에서 열로 변환할 수도 있습니다.
Import-Csv "C:\Result.csv" -delimiter "," | Sort-Object _from -Unique | Export-csv "C:\FINAL_REPORT_$date.csv"
언급URL : https://stackoverflow.com/questions/22275724/how-to-save-a-json-object-to-a-file-using-powershell
'programing' 카테고리의 다른 글
Open 생성 방법기존 Spring REST API의 API 3.0 YAML 파일? (0) | 2023.03.06 |
---|---|
TypeScript TS7015: 인덱스 식이 '번호' 유형이 아니므로 요소에 암묵적으로 '임의' 유형이 있습니다. (0) | 2023.03.06 |
각도로 페이지 제목을 설정하는 방법JS (0) | 2023.03.06 |
JsonResult를 반환하는 액션 메서드를 유닛 테스트하는 방법 (0) | 2023.03.06 |
스프링 부트 메모리 사용량을 줄이는 방법 (0) | 2023.03.06 |