PowerShell Code Indentation Issue While Updating JSON in Git

Coder 0 Reputation points
2025-04-04T16:34:41.8366667+00:00

The goal is to format the JSON output as { "google": "123" }, but the current output is showing as

{

"Google": "123" }.

What could be causing this indentation issue in the PowerShell code?

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,914 questions
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 35,726 Reputation points
    2025-04-05T23:42:25.0366667+00:00

    I tried to recreate what you were doing. Here's what I came up with.

    I used this test.json file as input.

    {                                                                                                                      
      "softwareCollectionId": [ 
    		{"Windows": "11"},
    		{"Google": "123"}
    	]
    }	
    
    

    Here's my test script.

    cls
    $InstallCollectionname = "firefox"
    $InstallCollectionID = "zrv1"
    $jsoncontent = Get-Content -Path "c:\temp\test.json" -Raw | ConvertFrom-Json
    "Starting collection count: {0}" -f $jsoncontent.softwareCollectionId.count
    $jsoncontent.softwareCollectionId += [PSCustomObject]@{$InstallCollectionname=$InstallCollectionID}
    "Collection count after add: {0}" -f $jsoncontent.softwareCollectionId.count
    ""
    "Json content"
    $jsoncontent | ConvertTo-Json -Depth 10 
    ""
    "Json content (compressed)"
    $jsoncontent | ConvertTo-Json -Depth 10 -Compress
    # Convert back to JSON and format correctly
    $jsonstring = $jsoncontent | ConvertTo-Json -Depth 10
    # Save the updated JSON
    $jsonstring | Set-Content -Path "c:\temp\testout.json" -Encoding UTF8
    
    
    

    User's image

    That produces this output file.

    User's image

    So I guess that you would need to use the "-compress" switch to consolidate the entries.

    If that's not what you want, then can you provide an input json file that recreates the problem?


  2. Rich Matheisen 47,786 Reputation points
    2025-04-06T02:50:55.1233333+00:00

    I suppose you could contrive some sort of editing of the output from ConvertFrom-JSON.

    This isn't exactly what you want, but you could use it as a starting point:

    # a = a single valued string
    # b = an array of hashes
    $coll = [PSCustomObject]@{
        a = "a string"
        b = @(@{google="abcd"},@{microsoft="efgh"})
    }
    $newentry = @{firefox="zrv1"}
    $coll.b += $newentry
    $j = ConvertTo-Json -InputObject $coll -Compress
    $j = $j -replace '},{', "},`n{" -replace '\[', "[`n" -replace '\]', "`n]"
    

    But you're really asking a question about aesthetics, not about how to produce well-formed JSON. You could try using YAML instead of JSON. That would give you data that looked like this:

    a: a string
    b:
    - google: abcd
    - microsoft: efgh
    - firefox: zrv1
    
    0 comments No comments

  3. Rich Matheisen 47,786 Reputation points
    2025-04-06T15:44:15.61+00:00

    I expect that the ConvertFrom-JSON cmdlet is using the System.Text.Json C# class. If that class can't format the JSON in the way you want, you're going to have to look for another JSON "pretty printer".

    There are several on-line sites that offer one, but I haven't tried any of them. Python and (I think) JavaScript also have JSON pretty print capabilities. So does Perl.

    If none of those can satisfy your format needs, you're going to have to edit the converted JSON objects' stringified output. That's no small undertaking.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.