简体   繁体   中英

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"
    }
]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM