Ich habe die folgende JSON-Datei in ein Powershell-Darstellungsobjekt konvertiert.
{
"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\"
}
]
}
Nach einigen Berechnungen mit Powershell-Darstellung möchte ich es als JSON in einer Datei speichern. Der Befehl $jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json"
speichert es jedoch auf diese Weise
{
"computer": [
{
"children": " ",
"full_path_on_file_sys": "T:\Dropbox\"
}
]
}
Frage: Wie erreicht man die korrekte Speicherung einer komplexen Powershell-JSON-Darstellung?
-depth Argument für ConvertTo-Json löst das Problem.
$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
Pipe es einfach an Set-Content oder Out-File:
Get-Process powershell |
ConvertTo-Json |
Set-Content json.txt
wenn Sie mit PowerShell Version 2 nicht weiterkommen, könnte das JSON-Modul von Joel Bennett aus dem 'PowerShell Code Repository' hilfreich sein.
$ json.properties.metadata | ConvertTo-Json -Compress
Wenn Sie die Ausgabe anzeigen und in einer Datei speichern möchten, können Sie den Befehl tee übermitteln.
Get-Process powershell | ConvertTo-Json | Tee-Object json.txt