DSC icon indicating copy to clipboard operation
DSC copied to clipboard

Malformed JSON output for `dsc resource get --all`

Open michaeltlombardi opened this issue 9 months ago • 4 comments

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

  1. Build DSC.

  2. 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

michaeltlombardi avatar Mar 28 '25 14:03 michaeltlombardi

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.

SteveL-MSFT avatar May 01 '25 22:05 SteveL-MSFT

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.

michaeltlombardi avatar May 02 '25 15:05 michaeltlombardi

When piping (like to ConvertFrom-Json, the default is json, not pretty-json

SteveL-MSFT avatar May 06 '25 22:05 SteveL-MSFT

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 avatar May 07 '25 13:05 michaeltlombardi

@michaeltlombardi this should be considered resolved with the new json-array output format?

SteveL-MSFT avatar Jun 20 '25 21:06 SteveL-MSFT