Home > Back-end >  Powershell saving elements in order to json file
Powershell saving elements in order to json file

Time:10-06

I currently am transferring an array to a json file. I want to be able to save the elements in order.

            {
                New-Object PSObject -Property @{
                    Name = $ADInfo.DisplayName
                    Username = $Username
                    Email = $Email
                    Date = $Date
                    Manager = $ManagerName
                    ManagerEmail = $ManagerEmail
                    Manager2 = $ManagerName2
                    ManagerEmail2 = $ManagerEmail2
                    Manager3 = $ManagerName3
                    ManagerEmail3 = $ManagerEmail3
                    ServiceTag = $ServiceTagTextBox.text
                    FedExSentTracking = "https://www.fedex.com/fedextrack/?tracknumbers="   $FedEXSentTextBox.text
                    FedExRecievedTracking = "https://www.fedex.com/fedextrack/?tracknumbers="   $FedEXRecievedTextBox.text


                }
            }

            $script:jsonDB  = $CollectionData

            $script:jsonDB | ConvertTo-Json -Compress  | 
            Set-Content C:\Support\HardwareCollection.json

When I save like this the output looks like the db below.

[{"ServiceTag":"1","Username":"RandomUser","Manager":"Random Manager","ManagerEmail3":"[email protected]","ManagerEmail":"[email protected]","Name":"Some User","ManagerEmail2":"[email protected]","Manager3":"Random Manager3","FedExSentTracking":"https://www.fedex.com/fedextrack/?tracknumbers=1","FedExRecievedTracking":"https://www.fedex.com/fedextrack/?tracknumbers=1","Manager2":"Random Manager 2","Date":"10/06/2021 09:17:24 -05:00","Email":"[email protected]"}]

How do I save the elements so that they save to the json file in the order in my first code block, as opposed to the unordered chaos in the second block.

Thank you very much!!!

CodePudding user response:

Instead of New-Object -Property, use the [pscustomobject] initializer syntax to create the object - this will retain the order of the properties specified:

[pscustomobject]@{
    Name                  = $ADInfo.DisplayName
    Username              = $Username
    Email                 = $Email
    Date                  = $Date
    Manager               = $ManagerName
    ManagerEmail          = $ManagerEmail
    Manager2              = $ManagerName2
    ManagerEmail2         = $ManagerEmail2
    Manager3              = $ManagerName3
    ManagerEmail3         = $ManagerEmail3
    ServiceTag            = $ServiceTagTextBox.text
    FedExSentTracking     = "https://www.fedex.com/fedextrack/?tracknumbers="   $FedEXSentTextBox.text
    FedExRecievedTracking = "https://www.fedex.com/fedextrack/?tracknumbers="   $FedEXRecievedTextBox.text
}

If you find yourself in a situation where you need to construct a dictionary of properties ahead of time and want to pass it to New-Object or [pscustomobject] at a later point in time, cast the hashtable literal to [ordered] - this will make PowerShell create an insert-ordered dictionary instead of a hashtable:

$properties = [ordered]@{
  Name = "John Doe"
}

# ...

# Add some more property definitions to the dictionary
$properties['Date'] = Get-Date
$properties['OtherStuff'] = "more information"

# both methods will now produce an object with the properties in the right order
New-Object PSObject -Property $properties
# or
[pscustomobject]$properties
  • Related