ConvertTo-Json
Use ConvertTo-Json to convert an object to a JSON-formatted string. See Microsoft.PowerShell.Utility module requirements and examples.
Documentation: Microsoft Learn — ConvertTo-Json
Syntax
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>]- Stage 1ConvertTo-Json
-InputObject -Depth -Compress
Try it
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.
Follow the objects
Run the command to inspect each stage.
Examples
Run 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
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
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.
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