0

If i have two json files, how can I combine the objects using powershell? I would like the values in the second file to supersede the values from the first object. There is possibly nested complex objects within the json. Really I'm looking for a modern solution to ($Json1 ConvertFrom-Json) | Merge ($Json2 ConvertFrom-Json) | ConvertTo-Json as Merge is deprecated, and i cannot use that in my azure pipeline.

{
"name" : "value",
"type": "someType",
"color": "green",
"someComplexObject" : value of complex object
}
{
"name" : "value1",
"color": "blue",
"someComplexObject" : value of complex object
}

Where the final result would be

{
"name" : "value1",
"type": "someType",
"color": "blue",
"someComplexObject" : value of complex object
}
4
  • 1
    Should $json2.someComplexObject just overwrite $json1.someComplexObject, or do you need to merge them recursively? Commented Apr 20, 2020 at 20:55
  • They'll need to also merge recursively Commented Apr 20, 2020 at 21:04
  • If the properties were all different, you could use convertfrom-json -ashashtable, then add the hashtables. Commented Apr 20, 2020 at 23:09
  • 1
    Here's a jq solution: stackoverflow.com/questions/19529688/… Commented Apr 20, 2020 at 23:34

1 Answer 1

-2

Merge/Concatenate multiple json files using PowerShell

TL;DR

@($JSON1; $JSON2) | ConvertTo-Json -Depth 20
Sign up to request clarification or add additional context in comments.

1 Comment

This solution does not merge 2 objects as described by the OP. It would create an array with both objects intact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.