Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge/Concatenate multiple json files using PowerShell

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"
    }
]
like image 460
JadonR Avatar asked Aug 31 '18 00:08

JadonR


People also ask

How do I combine multiple JSON objects into one?

We can merge two JSON objects using the putAll() method (inherited from interface java.

Can we merge two JSON files?

Merge two JSON files without using a third file in Python You can do it by importing json library but it would be a little complex for beginners who have no idea about json objects and Python dictionary. So, here we will do it using basic File Handling in Python as it will be much easier for you!

Can PowerShell parse JSON?

PowerShell makes it easy to modify JSON by converting JSON to a PSCustomObject. The object can then be modified easily like any other object. The object can then be exported back out using ConvertTo-Json.


1 Answers

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.

like image 105
Maximilian Burszley Avatar answered Oct 21 '22 14:10

Maximilian Burszley