54

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?

6 Answers 6

107

-depth argument for ConvertTo-Json solves the issue.

$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
Sign up to request clarification or add additional context in comments.

4 Comments

Just a quick note for anyone wondering - the default depth is 2, if it's not specified in ConvertTo-Json -depth x
I tried this out today and noticed that the default Out-File produced an unusable file (encoded in UTF-16) 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
The character encoding from Out-File is specified with the -Encoding parameter.
Since Powershell Core 6.0, the default encoding is UTF-8 w/ BOM (rather than UTF-16). See more here: learn.microsoft.com/en-us/powershell/scripting/whats-new/…
15

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

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

1 Comment

Set-Content did it for me.
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

Comments

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

Comments

1

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

2 Comments

The code is not available anymore at the specified url
-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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.