47

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

{
"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

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

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

Puzik
  • 1,423
  • 1
  • 10
  • 11

6 Answers6

94

-depth argument for ConvertTo-Json solves the issue.

$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
Puzik
  • 1,423
  • 1
  • 10
  • 11
  • 9
    Just a quick note for anyone wondering - the default depth is 2, if it's not specified in `ConvertTo-Json -depth x` – LeeM Jan 11 '18 at 06:17
  • 4
    I tried this out today and noticed that the default `Out-File` produced an unusable file ([encoded in UTF-16](https://stackoverflow.com/questions/10655788/powershell-set-content-and-out-file-what-is-the-difference)) that failed on a subsequent programmatic import in a gulp task. Looking at a Hex editor made this clear (and the JSON file also did not highlight properly with Intellisense in VSCode). I took my following failing code `$constants | ConvertTo-Json -depth 100 | Out-File $outFilePath` and replaced it with `$constants | ConvertTo-Json -depth 100 | Set-Content $outFilePath` – Simon Jan 18 '18 at 02:29
  • 4
    The character encoding from `Out-File` is specified with the `-Encoding` parameter. – lit Apr 21 '18 at 19:24
  • 1
    Since Powershell Core 6.0, the default encoding is UTF-8 w/ BOM (rather than UTF-16). See more here: https://learn.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-core-60?view=powershell-6#default-encoding-is-utf-8-without-a-bom-except-for-new-modulemanifest – hyperupcall Jun 23 '19 at 21:34
13

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

Get-Process powershell |
 ConvertTo-Json | 
 Set-Content json.txt
mjolinor
  • 66,130
  • 7
  • 114
  • 135
4

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
Jim Wolff
  • 5,052
  • 5
  • 34
  • 44
2
$json.properties.metadata | ConvertTo-Json -Compress
Pang
  • 9,564
  • 146
  • 81
  • 122
Peter
  • 148
  • 1
  • 8
1

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

user4531
  • 2,525
  • 7
  • 30
  • 38
-2

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"