Powershell: ConvertTo-Json

Created on 15 Feb 2020  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

@('a')|convertto-json

Expected behavior

[                                                                 
"a"                                                                                                   
]

Actual behavior

"a"

Environment data


Area-Cmdlets-Utility Issue-Question Resolution-Answered

All 4 comments

The pipeline by nature unwraps arrays into individual items. To a receiving cmdlet, there is no distinguishable difference between these two scenarios:

'a' | ConvertTo-Json

@('a') | ConvertTo-Json

If you need single-item arrays you will need to pass them directly to the -InputObject parameter rather than using the pipeline.

Not sure if I should open up a new ticket or glue my similar issue to this one, so I'll go with the former and may the mods hit me over the head with a clue-stick if necessary :)

This is one thing I think ConvertTo-Json should handle:

PS /> ps
PID TTY TIME CMD
24618 pts/0 00:00:00 sudo
24622 pts/0 00:00:00 su
24623 pts/0 00:00:00 bash
25724 pts/0 00:00:01 pwsh
25757 pts/0 00:00:00 ps
PS ACTUAL> ps | ConvertTo-Json
[
" PID TTY TIME CMD",
"24618 pts/0 00:00:00 sudo",
"24622 pts/0 00:00:00 su",
"24623 pts/0 00:00:00 bash",
"25724 pts/0 00:00:01 pwsh",
"26184 pts/0 00:00:00 ps"
]
PS EXPECTED> ps | ConvertTo-Json
[
{
"PID": 24618,
"TTY": "pts/0",
"TIME": "00:00:00",
"CMD": "sudo"
},
{
"PID": 24622,
"TTY": "pts/0",
"TIME": "00:00:00",
"CMD": "su"
},
{
"PID": 24623,
"TTY": "pts/0",
"TIME": "00:00:00",
"CMD": "bash"
},
{
"PID": 25724,
"TTY": "pts/0",
"TIME": "00:00:00",
"CMD": "pwsh"
},
{
"PID": 26184,
"TTY": "pts/0",
"TIME": "00:00:00",
"CMD": "ps"
}
]

Because if it doesn't, then by default it could be replaced with something like this simple awk script :)

awk 'BEGIN{printf("["); c="n";}{printf("%st"%s"",c,$0); c=",n"; }END{print "n]";}'

user@wslhost:/mnt/c/Users/user$ ps | awk 'BEGIN{printf("["); c="n";}{printf("%st"%s"",c,$0); c=",n"; }END{print "n]";}'
[
" PID TTY TIME CMD",
" 218 pts/4 00:00:00 bash",
" 269 pts/4 00:00:00 ps",
" 270 pts/4 00:00:00 awk"
]

But then I may be overreaching?

@thorsig bit of a different issue there. The alias ps that points to Start-Process in Windows editions of PowerShell (including recent versions of PowerShell 7 GA) is not actually present in linux, due to there being a native ps executable that many folks use.

That executable only ever emits string data, so ConvertTo-Json can only treat it as such.

What you seem to be wanting is still achievable with Get-Process | ConvertTo-Json. 馃檪

If you instead want the text output from ps to be interpreted as some kind of object, you need to provide that conversion yourself; cmdlets in PowerShell can't afford to make any assumptions about the expected format of string data without breaking other users' assumptions in the process. It's for a similar reason that many very useful aliases were removed for Unix editions of PowerShell; many Unix users got very upset that PowerShell was "taking over" these native executables with its aliases and producing (for them) unexpected behaviour when using familiar commands.

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings