-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMerge-JSON.ps1
37 lines (36 loc) · 1.39 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
function Join-Objects($source, $extend){
if($source.GetType().Name -eq "PSCustomObject" -and $extend.GetType().Name -eq "PSCustomObject"){
foreach($Property in $source | Get-Member -type NoteProperty, Property){
if($null -eq $extend.$($Property.Name)){
continue;
}
$source.$($Property.Name) = Join-Objects $source.$($Property.Name) $extend.$($Property.Name)
}
}else{
$source = $extend;
}
return $source
}
function AddPropertyRecurse($source, $toExtend){
if($source.GetType().Name -eq "PSCustomObject"){
foreach($Property in $source | Get-Member -type NoteProperty, Property){
if($null -eq $toExtend.$($Property.Id)){
$toExtend | Add-Member -MemberType NoteProperty -Value $source.$($Property.Id) -Name $Property.Id `
}
else{
$toExtend.$($Property.Id) = AddPropertyRecurse $source.$($Property.Id) $toExtend.$($Property.Id)
}
}
}
return $toExtend
}
function Merge-JSON($source, $extend){
$merged = Join-Objects $source $extend
$extended = AddPropertyRecurse $source $merged
$extended
}
#read json files into PSCustomObjects like this:
#$1 = Get-Content 'C:\1.json' -Raw | ConvertFrom-Json
#$2 = Get-Content 'C:\2.json'-Raw | ConvertFrom-Json
#Merge properties of the first one and second one.
#$3 = Json-Merge $1 $2