dharmatech
dharmatech

Reputation: 9527

ConvertTo-Json on array read from file yields unexpected results

Write an array to a file:

PS C:\Users\dharmatech\Downloads> 10, 20, 30 | Out-File 'test.txt'

Read the array back in and call ConvertTo-Json on it:

PS C:\Users\dharmatech\Downloads> ConvertTo-Json (Get-Content .\test.txt)

The result is not the JSON version of the array... See the result below.

If you call Get-Member on one of the strings in the array read from the file, you'll see that the string object has been annotated with a few NotePropertys. I'm guessing that explains the ConvertTo-Json output.

What's a good way to get the JSON version of the array in the file, using ConvertTo-JSON?

[
    {
        "value":  "10",
        "PSPath":  "C:\\Users\\dharmatech\\Downloads\\test.txt",
        "PSParentPath":  "C:\\Users\\dharmatech\\Downloads",
        "PSChildName":  "test.txt",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\dharmatech\\Downloads",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "",
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\dharmatech",
                           "Drives":  "C D"
                       },
        "ReadCount":  1
    },
    {
        "value":  "20",
        "PSPath":  "C:\\Users\\dharmatech\\Downloads\\test.txt",
        "PSParentPath":  "C:\\Users\\dharmatech\\Downloads",
        "PSChildName":  "test.txt",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\dharmatech\\Downloads",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "",
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\dharmatech",
                           "Drives":  "C D"
                       },
        "ReadCount":  2
    },
    {
        "value":  "30",
        "PSPath":  "C:\\Users\\dharmatech\\Downloads\\test.txt",
        "PSParentPath":  "C:\\Users\\dharmatech\\Downloads",
        "PSChildName":  "test.txt",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\dharmatech\\Downloads",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "",
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\dharmatech",
                           "Drives":  "C D"
                       },
        "ReadCount":  3
    }
]
PS C:\Users\dharmatech\Downloads>

Upvotes: 0

Views: 1165

Answers (2)

Keith Hill
Keith Hill

Reputation: 201652

Get-Content has a bad habit of extending the strings it outputs with a whole lot of extra information. This can cause memory bloat when dealing with large files. I wish Get-Content had a switch to enable these extra properties when needed which would be rare. However, as this might break existing programs it is more feasible to ask for a parameter that strips off the extra properties. Perhaps something like ValueOnly?

To get around these problems you can cast as mjolinor shows above but for the best performance, drop down to .NET:

ConvertTo-Json ([IO.File]::ReadAllLines("$pwd\test.txt"))

[
    "10",
    "20",
    "30"
]

While this is unnecessary for anything but really large files, it is a good trick to have in your toolbox.

Upvotes: 2

mjolinor
mjolinor

Reputation: 68273

This seems to work:

10, 20, 30 | Out-File 'testfile1.txt'

convertto-json ([string[]](gc testfile1.txt))

[
    "10",
    "20",
    "30"
]

Or, if you want them as [int]

10, 20, 30 | Out-File 'testfile1.txt'

convertto-json ([int[]](gc testfile1.txt))

[
    10,
    20,
    30
]

Upvotes: 3

Related Questions