6

I have multiple .json files that I would like to concatenate or append to each other using PowerShell. Where one will end, I would like the next to continue. To keep it simple, I'm just going to show an example of two files that I would like to merge.

File1.json

[
    {
        "ItemID":  10746,
        "CompanyID":  3694,
        "Company":  "Sweet Mamma",
        "SRP":  0.0001,
        "UPC":  "9076625"
    },
    {
        "ItemID":  10761,
        "CompanyID":  3694,
        "Company":  "Sweet Mamma",
        "UPC":  "6128021"
    } ]

File2.json

[
    {
        "ItemID":  477761,
        "CompanyID":  4398,
        "Company":  "Moonlight",
        "UPC":  "4000308"
    },
    {
        "ItemID":  477761,
        "CompanyID":  4398,
        "Company":  "Moonlight",
        "SRP":  14.6500,
        "UPC":  "099904000308"
    }
]

I know that I can use the below syntax in Powershell to concatenate the two json files.

Get-Content "C:\File1.json","C:\File2.json" | Set-Content "C:\CombinedOutput.json"

However, when I execute this script, I nearly get what I need, except the beginning and ending brackets are left (where one json file ends and the next begins). These two brackets as shown in the example below in the middle, which should be removed and replaced with a comma as shown in the above example.

Note: I was unable to bold so I am drawing attention to them by surrounding with asterisks.

[
    {
        "ItemID":  10746,
        "CompanyID":  3694,
        "Company":  "Sweet Mamma",
        "SRP":  0.0001,
        "UPC":  "9076625"
    },
    {
        "ItemID":  10761,
        "CompanyID":  3694,
        "Company":  "Sweet Mamma",
        "UPC":  "6128021"
    }
**]**
**[**
    {
        "ItemID":  477761,
        "CompanyID":  4398,
        "Company":  "Moonlight",
        "UPC":  "4000308"
    },
    {
        "ItemID":  477761,
        "CompanyID":  4398,
        "Company":  "Moonlight",
        "SRP":  14.6500,
        "UPC":  "099904000308"
    }
]

To reiterate, these brackets...

]
[

...should be replaced with a comma.

,

The desired output file would then look like the below and even merging several json files into one would still allow it to properly flow like one continuous json file.

[
    {
        "ItemID":  10746,
        "CompanyID":  3694,
        "Company":  "Sweet Mamma",
        "SRP":  0.0001,
        "UPC":  "9076625"
    },
    {
        "ItemID":  10761,
        "CompanyID":  3694,
        "Company":  "Sweet Mamma",
        "UPC":  "6128021"
    },
    {
        "ItemID":  477761,
        "CompanyID":  4398,
        "Company":  "Moonlight",
        "UPC":  "4000308"
    },
    {
        "ItemID":  477761,
        "CompanyID":  4398,
        "Company":  "Moonlight",
        "SRP":  14.6500,
        "UPC":  "099904000308"
    }
]
1

1 Answer 1

17

This is actually surprisingly easy since they're both arrays:

$js1 = Get-Content -Path .\file1.json -Raw |
    ConvertFrom-Json
$js2 = Get-Content -Path .\file2.json -Raw |
    ConvertFrom-Json

$js1 + $js2 |
    ConvertTo-Json -Depth 5 |
    Out-File -FilePath .\combinedfiles.json

PowerShell can concatenate arrays natively here.

4
  • Brilliant! This appears to work. I'll play with it more to be sure it's what I need but so far it looks great. Thanks for the quick reply! You guys are geniuses!
    – JadonR
    Aug 31, 2018 at 0:55
  • @JadonR Glad to be of service. If this ends up resolving your question, don't forget to mark it as such ✅ Aug 31, 2018 at 0:57
  • I would just like to add to this answer that if you have objects that aren't arrays already you can just cast them to arrays before using this technique @($js1) + @($js2) | ConvertTo-Json Nov 11, 2019 at 10:16
  • 6
    @DavidSöderlund Simpler still: @($js1; $js2). This is the array subexpression operator and will turn any contents into a single array. Jan 27, 2020 at 17:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.