Skip to content

ConvertTo-Json: implement "streaming" mode #26533

Open

Description

Summary of the new feature / enhancement

Currently, the ConvertTo-Json cmdlet assembles all input objects into an in-memory List, then converts that to one Json string for output. This can be inefficient if there are many input objects.

Enhancing ConvertTo-Json to (optionally) convert each input object to a complete Json string and output
as it is processed offers these benefits:

  • reduced memory usage
  • substantially improved throughput when the output stream is being processed by a separate thread
    or process.

Proposed technical implementation details (optional)

Add an AsStream parameter to the cmdlet and modify the code to convert and output each object individually.

Activity

  1. paulhilchey commented on Nov 26, 2025

    @paulhilchey
    Author

    Pull request with the code changes: #25597
    Related documentation issue: MicrosoftDocs/PowerShell-Docs#12104

  2. mklement0 commented on Nov 27, 2025

    @mklement0
    Contributor

    Good idea, but I suggest calling the parameter -Stream instead, analogous to Out-String's conceptually related parameter.

    It's also worth spelling out how this will affect the output for multiple input objects:

    • 'a', 'b' | ConvertTo-Json -Compress yields a single output string (irrespective of the number of input objects) that is a JSON array, verbatim ["a","b"]

    • 'a', 'b' | ConvertTo-Json -Stream -Compress will yield two output strings (that will be streamed as they become available), verbatim "a" and "b" (which when captured will become @('"a"', '"b"'))

  3. paulhilchey commented on Dec 2, 2025

    @paulhilchey
    Author

    I'm happy to call the parameter -Stream if that is the consensus.
    I'll see about documenting the behaviour with -Compress too.

  4. iSazonov commented on Dec 2, 2025

    @iSazonov
    Collaborator

    We need WG approve before PR.

  5. mklement0 commented on Dec 2, 2025

    @mklement0
    Contributor

    Thanks, Paul Hilchey (@paulhilchey), but note that -Compress is incidental to the issue at hand; I merely used it to show the current output for multi-object input more concisely.

  6. iSazonov commented on Dec 4, 2025

    @iSazonov
    Collaborator

    #25597 (comment)

    This was approved by maintainers with the wish to have this parameter for ConvertFrom-Json too.

    Paul Hilchey (@paulhilchey) Please update the issue title and description.

  7. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    WG-ReviewedA Working Group has reviewed this and made a recommendation
    on Dec 4, 2025
  8. jborean93 commented on Dec 9, 2025

    @jborean93
    Collaborator

    Please update the issue title and description.

    There's a few questions/issues I have with the ConvertFrom-Json request

    • Is there a good reason to block just doing ConvertTo-Json for this
    • Why not have it as a separate issue and add it based on demand like other features

    A bigger question is what does -Stream add to ConvertFrom-Json that it doesn't already have? For example it already streams in a way by emitting each individual objects from the input

    $res = '{"key": 1}', '{"key": 2}' | ConvertFrom-Json
    $res.Count 
    # 2
    
    $res
    # key
    # ---
    #   1
    #   2

    The only thing I can think of is to emit the objects as they are processed but I cannot see why that just can't be the default behaviour and why a switch would be needed for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions