forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge-Json.ps1
47 lines (39 loc) · 1.11 KB
/
Merge-Json.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Create a new JSON string by recursively combining the properties of JSON strings.
.INPUTS
System.String of JSON to combine.
.OUTPUTS
System.String of JSON combining the inputs.
.LINK
Merge-PSObject.ps1
.LINK
ConvertFrom-Json
.LINK
ConvertTo-Json
.EXAMPLE
'{"a":1,"b":{"u":3},"c":{"v":5}}','{"a":{"w":8},"b":2,"c":{"x":6}}' |Merge-Json.ps1
{
"a": {
"w": 8
},
"b": 2,
"c": {
"v": 5,
"x": 6
}
}
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])] Param(
<#
JSON string to combine. Descendant properties are recursively merged.
Primitive values are overwritten by any matching ones in the new JSON string.
#>
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromRemainingArguments=$true)][string[]] $InputObject,
# Omits white space and indented formatting in the output string.
[switch]$Compress
)
Begin {$value = [pscustomobject]@{}}
Process {$value = $value,($InputObject |ConvertFrom-Json) |Merge-PSObject.ps1}
End {$value |ConvertTo-Json -Compress:$Compress}