Skip to content

Commit

Permalink
feat: Out-JSON ( Fixes #727 )
Browse files Browse the repository at this point in the history
    Adding Example
  • Loading branch information
StartAutomating authored and StartAutomating committed Mar 9, 2024
1 parent 0f12f4b commit 56f225e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Commands/JSON/Out-JSON.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[ValidatePattern("JSON")]
param()

function Out-JSON {

<#
.SYNOPSIS
Outputs objects as JSON
.DESCRIPTION
Outputs objects in JSON.
If only one object is outputted, it will not be in a list.
If multiple objects are outputted, they will be in a list.
.LINK
@{
a = 'b'
} | Out-JSON
#>
[CmdletBinding(PositionalBinding=$false)]
[Alias('text.json.out')]
param(
# The input object. This will form the majority of the JSON.
[Parameter(ValueFromPipeline)]
[PSObject]
$InputObject,

# Any arguments. These will be appended to the input.
[Parameter(ValueFromRemainingArguments=$true)]
[PSObject[]]
$ArgumentList,

# The depth of the JSON. By default, this is the FormatEnumerationLimit.
[int]
$Depth = $FormatEnumerationLimit
)

$inputAndArguments = @( $input ) + $ArgumentList
if (-not $depth) {
$depth = if ($FormatEnumerationLimit) {
$FormatEnumerationLimit
} else {
4
}
}
if ($inputAndArguments.Length -eq 1) {
ConvertTo-Json -InputObject $inputAndArguments[0] -Depth $Depth
} else {
$inputAndArguments | ConvertTo-Json -Depth $Depth
}

}

0 comments on commit 56f225e

Please sign in to comment.