Malformed JSON output for `dsc resource get --all`
Prerequisites
- [x] Write a descriptive title.
- [x] Make sure you are able to repro it on the latest version
- [x] Search the existing issues.
Summary
The output for the dsc resource get command with the --all option emits a series of JSON objects without a wrapping array. For both the compressed (default, JSON Line) and pretty JSON output formats, the command emits each object directly after the last. It doesn't include a newline between objects, and the items aren't wrapped in an array.
This makes the output view difficult to parse and prevents users from leveraging tools like jq or the ConvertFrom-Json cmdlet.
Steps to reproduce
-
Build DSC.
-
Invoke the following commands:
# Output to terminal dsc resource get Microsoft/Process --all --output-format pretty-json # Convert streaming results dsc resource get Microsoft/Process --all --output-format pretty-json | ConvertFrom-Json
Expected behavior
dsc resource get Microsoft/Process --all --output-format pretty-json
[
{
"actualState": {
"pid": 4136,
"name": "\"svchost.exe\"",
"cmdline": "[]"
}
},
{
"actualState": {
"pid": 22860,
"name": "\"RuntimeBroker.exe\"",
"cmdline": "[]"
}
}
]
Actual behavior
dsc resource get Microsoft/Process --all --output-format pretty-json
{
"actualState": {
"pid": 4136,
"name": "\"svchost.exe\"",
"cmdline": "[]"
}
}{
"actualState": {
"pid": 22860,
"name": "\"RuntimeBroker.exe\"",
"cmdline": "[]"
}
}
Error details
Environment data
Name Value
---- -----
PSVersion 7.5.0
PSEdition Core
GitCommitId 7.5.0
OS Microsoft Windows 10.0.26100
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Version
Build from main
Visuals
No response
This currently works similar to dsc resource list -o json in that it emits JSONLines. Does it make sense to change this to be an array? Alternatively, we could add a --as-array (or something like that) to both cases to get the behavior you want but make it opt-in.
I think returning JSONLines for -o json is perfectly reasonable - this only really applies when the output format is -o pretty-json. For YAML I think we already added some handling to emit the YAML with document separators, so maybe we should have some handling for emitting pretty-json when we know the output can be multi-object.
When piping (like to ConvertFrom-Json, the default is json, not pretty-json
Agreed - but of the three output formats, only pretty-json is invalid - json returns JSONLines, yaml returns a series of YAML documents with the --- separator, and pretty-json returns a series of indented JSON objects without separating newlines or commas, and not wrapped in an array.
The pretty-json output is confusing to read in the terminal (which is imo the primary usage for that format) and fails to parse either piped or saved to a variable/temporary file.
@michaeltlombardi this should be considered resolved with the new json-array output format?