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

后端 未结 6 568
情话喂你
情话喂你 2021-02-01 15:10

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

{
\"computer\": [
    {
        \"children\": [   
            {   
                        


        
相关标签:
6条回答
  • 2021-02-01 15:20

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

    Get-Process powershell | ConvertTo-Json |  Tee-Object json.txt
    
    0 讨论(0)
  • 2021-02-01 15:24

    -depth argument for ConvertTo-Json solves the issue.

    $jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
    
    0 讨论(0)
  • 2021-02-01 15:25

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

    Get-Process powershell |
     ConvertTo-Json | 
     Set-Content json.txt
    
    0 讨论(0)
  • 2021-02-01 15:26
    $json.properties.metadata | ConvertTo-Json -Compress
    
    0 讨论(0)
  • 2021-02-01 15:27

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

    0 讨论(0)
  • 2021-02-01 15:41

    1) The below command can be used to convert a json to 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: 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.

    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:

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

    0 讨论(0)
提交回复
热议问题