PowerShell Genie

ConvertTo-Json

Use ConvertTo-Json to convert an object to a JSON-formatted string. See Microsoft.PowerShell.Utility module requirements and examples.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — ConvertTo-Json

Syntax

1PS> ConvertTo-Json [-InputObject] <Object> [-Depth <Int32>] [-Compress] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

ConvertTo-Json [-InputObject] <Object> [-Depth <Int32>] [-Compress] [<CommonParameters>]
  1. Stage 1ConvertTo-Json-InputObject -Depth -Compress

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Run ConvertTo-Json

    1PS> ConvertTo-Json
    Show reference output

    Sample output · fictional data, shaped like the real thing

    {
      "Server": "SRV01",
      "Status": "Healthy",
      "Checked": "2026-09-10T22:31:00+05:30"
    }

    ConvertTo-Json -Verbose

    1PS> ConvertTo-Json -Verbose
    Show reference output

    Sample output · fictional data, shaped like the real thing

    {
      "Server": "SRV01",
      "Status": "Healthy",
      "Checked": "2026-09-10T22:31:00+05:30"
    }

    Parameters

    Parameter definitions:

    • -InputObject
    • -Depth
    • -Compress

    ConvertTo-Json on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use ConvertTo-Json when you need to convert an object to a JSON-formatted string. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: Nested values can be truncated when the configured depth is too small. Set an appropriate -Depth and inspect the resulting JSON instead of accepting a warning as successful full serialization. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: ConvertFrom-Json. ConvertTo-Json serializes PowerShell values to JSON text; ConvertFrom-Json parses JSON into values. Keep the original object when later stages need typed properties rather than serialized text. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    ConvertTo-Json reference on Microsoft Learn

    Recap

    ConvertTo-Json Use ConvertTo-Json to convert an object to a JSON-formatted string. See Microsoft.PowerShell.Utility module requirements and examples.

    Next: ConvertFrom-Json
    Genie guideConvertTo-Json

    Use ConvertTo-Json to convert an object to a JSON-formatted string. The console below is pre-loaded — run it, then change a value and run it again.