Skip to content

Format-SpectreJson

Description

This function takes an objects and converts them into syntax highlighted Json using the Spectre Console Json Library.
Thanks to trackd for adding this!
See https://spectreconsole.net/widgets/json for more information.


Examples

Example 1
This example demonstrates how to format an array of objects into a Spectre Console Json object with syntax highlighting.

Terminal window
$data = @(
[pscustomobject]@{
Name = "John"
Age = 25
City = "New York"
IsEmployed = $true
Salary = 10
Hobbies = @("Reading", "Swimming")
Address = @{
Street = "123 Main St"
ZipCode = $null
}
}
)
Format-SpectreJson -Data $data

Example 2
This example demonstrates how to display json string data using Format-SpectreJson.

Terminal window
$jsonString = @{
Name = 'Alice'
Age = 30
Skills = @('PowerShell', 'Spectre.Console')
} | ConvertTo-Json
# Display the JSON string using Format-SpectreJson
$jsonString | Format-SpectreJson

Example 3
This example demonstrates how to display the contents of one or more JSON files directly using Format-SpectreJson. Create temporary JSON files for demonstration, use a random execution ID to avoid reading random json files from the temp directory.

Terminal window
$executionId = [guid]::NewGuid().ToString()
$tempJson1 = (New-TemporaryFile).FullName -replace '.tmp$', ".$executionId.json"
$tempJson2 = (New-TemporaryFile).FullName -replace '.tmp$', ".$executionId.json"
@{
Name = 'Alice'
Age = 30
Skills = @('PowerShell', 'Spectre.Console')
} | ConvertTo-Json | Set-Content -Path $tempJson1
@{
Name = 'Bob'
Age = 35
Skills = @('C#', 'JavaScript')
} | ConvertTo-Json | Set-Content -Path $tempJson2
# Display the JSON files directly using Get-ChildItem and Format-SpectreJson
$parent = Split-Path $tempJson1 -Parent
Get-ChildItem $parent -Filter "*.$executionId.json" | Format-SpectreJson

Parameters

Data

The array of objects to be formatted into Json.

TypeRequiredPositionPipelineInput
[Object]true1true (ByValue)

Depth

The maximum depth of the Json. Default is defined by the version of powershell.

TypeRequiredPositionPipelineInput
[Int32]false2false

NoBorder

TypeRequiredPositionPipelineInput
[Switch]falsenamedfalse

Border

TypeRequiredPositionPipelineInput
[String]false3false

Color

TypeRequiredPositionPipelineInput
[String]false4false

Title

TypeRequiredPositionPipelineInput
[String]false5false

Width

TypeRequiredPositionPipelineInput
[Int32]false6false

Height

TypeRequiredPositionPipelineInput
[Int32]false7false

JsonStyle

A hashtable of Spectre Console color names and values to style the Json output. e.g.

@{
MemberStyle = "Yellow"
BracesStyle = "Red"
BracketsStyle = "Orange1"
ColonStyle = "White"
CommaStyle = "White"
StringStyle = "White"
NumberStyle = "Red"
BooleanStyle = "LightSkyBlue1"
NullStyle = "Gray"
}
TypeRequiredPositionPipelineInput
[Hashtable]false8false

Syntax

Terminal window
Format-SpectreJson [-Data] <Object> [[-Depth] <Int32>] [-NoBorder] [[-Border] <String>] [[-Color] <String>] [[-Title] <String>] [[-Width] <Int32>] [[-Height] <Int32>] [[-JsonStyle] <Hashtable>] [<CommonParameters>]