如何使用 Powershell 将 JSON 对象保存到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22275724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:11:19  来源:igfitidea点击:

How to save a JSON object to a file using Powershell?

jsonfilepowershell

提问by Puzik

I have converted the following JSON file to powershell representation object.

我已将以下 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\"
    }
]

}

}

After doing some computations with powershell representation I would like to save it to file as JSON. But command $jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json"saves it in this way

在使用 powershell 表示进行一些计算后,我想将其保存为 JSON 文件。但是命令$jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json"以这种方式保存它

{
    "computer":  [
                     {
                         "children":  " ",
                         "full_path_on_file_sys":  "T:\Dropbox\"
                     }
                 ]
}

Question: how to achieve correct saving of complex powershell JSON representation?

问题:如何正确保存复杂的 powershell JSON 表示?

回答by Puzik

-depth argument for ConvertTo-Json solves the issue.

ConvertTo-Json 的 -depth 参数解决了这个问题。

$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"

回答by mjolinor

Just pipe it to Set-Content, or Out-File:

只需将其通过管道传输到 Set-Content 或 Out-File:

Get-Process powershell |
 ConvertTo-Json | 
 Set-Content json.txt

回答by Peter

$json.properties.metadata | ConvertTo-Json -Compress

回答by user4531

if you are stuck with PowerShell Version 2, the JSON module of Joel Bennett from the 'PowerShell Code Repository'might help.

如果您坚持使用 PowerShell 版本 2,则来自“PowerShell 代码存储库”的 Joel Bennett 的 JSON 模块可能会有所帮助。

回答by Jim Wolff

If you want to both view the output and save it to file, you can pipe the tee command.

如果您想同时查看输出并将其保存到文件,您可以通过管道传输 tee 命令。

Get-Process powershell | ConvertTo-Json |  Tee-Object json.txt

回答by sameervsarma

1) The below command can be used to convert a json to CSV

1) 以下命令可用于将 json 转换为 CSV

Example: Get-Content package.json | Out-String | ConvertFrom-Json | Select parameter1, parameter2, parameter3 | ConvertTo-Csv -NoTypeInformation | Format-Table >> C:\JenkinsWorkspace\Result.csv

例子: Get-Content package.json | Out-String | ConvertFrom-Json | Select parameter1, parameter2, parameter3 | ConvertTo-Csv -NoTypeInformation | Format-Table >> C:\JenkinsWorkspace\Result.csv

Get-Content: This is like "cat" command in linux which will get all data of file "package.json" and converts from Json (Using ConvertFrom-Json function) extracting the details of only required parameters and then converting them into CSV using "ConvertTo-Csv" function without any unwanted Type Headers and formatting them into Table.

Get-Content:这就像 linux 中的“cat”命令,它将获取文件“package.json”的所有数据并从 Json 转换(使用 ConvertFrom-Json 函数)仅提取所需参数的详细信息,然后使用“ConvertTo-Csv”函数没有任何不需要的类型标题并将它们格式化为表格。

2) The above result can also be formatted into proper csv to view it as Excel format without any duplicates and also having Text-To-Column conversion using below command:

2) 上面的结果也可以格式化为适当的 csv 以查看它作为 Excel 格式,没有任何重复,也可以使用以下命令进行文本到列的转换:

Import-Csv "C:\Result.csv" -delimiter "," | Sort-Object _from -Unique | Export-csv "C:\FINAL_REPORT_$date.csv"

Import-Csv "C:\Result.csv" -delimiter "," | Sort-Object _from -Unique | Export-csv "C:\FINAL_REPORT_$date.csv"