Skip to main content

Compare-MtJsonObject

SYNOPSIS

Compares two PowerShell objects (typically JSON objects) and returns a list of differences.

SYNTAX

Compare-MtJsonObject [-Baseline] <Object> [-Current] <Object> [[-Path] <String>] [[-Settings] <Object>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

The Compare-MtJsonObject function recursively compares two objects (such as those imported from JSON) and returns an array of differences. It supports comparison of nested objects, arrays, and allows exclusion of specific properties via the Settings parameter. The function is useful for configuration drift detection, regression testing, or validating changes between baseline and current states.

EXAMPLES

EXAMPLE 1

# Compare two JSON files and output the differences
$baseline = Get-Content -Raw -Path 'baseline.json' | ConvertFrom-Json
$current = Get-Content -Raw -Path 'current.json' | ConvertFrom-Json
$diffs = Compare-MtJsonObject -Baseline $baseline -Current $current
$diffs | Format-Table

EXAMPLE 2

# Exclude specific properties from comparison
$settings = [PSCustomObject]@{ ExcludeProperties = @('timestamp', 'lastModified') }
$diffs = Compare-MtJsonObject -Baseline $baseline -Current $current -Settings $settings

PARAMETERS

-Baseline

The reference object to compare against (e.g., the expected or original state).

Type: Object
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Current

The object to compare to the baseline (e.g., the actual or new state).

Type: Object
Parameter Sets: (All)
Aliases:

Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Path

(Optional) The property path being compared. Used internally for recursion and reporting.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Settings

(Optional) An object that may contain an ExcludeProperties property (array of property names to skip).

Type: Object
Parameter Sets: (All)
Aliases:

Required: False
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

[MtPropertyDifference[]] Returns an array of objects describing each difference found.

NOTES

Author: Stephan van Rooij @svrooij Date: 2025-06-26

https://maester.dev/docs/commands/Compare-MtJsonObject