Combine Two JSON Files With PowerShell
Over the years I’ve used a few different methods to combine two JSON files using PowerShell, but today I found a neat way to do it, so thought I would document it here for easy recall next time I need to do it.
Consider two basic JSON files to use for an example:
It’s easy to start working with them as objects in PowerShell, but I always forget that you can’t then just concatenate them together since they are PSCustomObjects:
The solution is to use an array instead. Today I found a neat solution in this StackOverflow post which uses the Array subexpression operator, rather than more long winded efforts I’ve used before:
The resulting combinedfruit.json file: