简体   繁体   English

使用 PowerShell 合并/连接多个 json 文件

[英]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.我有多个 .json 文件,我想使用 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文件1.json

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

File2.json文件2.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.我知道我可以在 Powershell 中使用以下语法来连接两个 json 文件。

Get-Content "C:\\File1.json","C:\\File2.json" |获取内容 "C:\\File1.json","C:\\File2.json" | Set-Content "C:\\CombinedOutput.json"设置内容 "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).然而,当我执行这个脚本时,我几乎得到了我需要的东西,除了开始和结束括号(一个 json 文件结束,下一个开始)。 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.所需的输出文件将如下所示,即使将多个 json 文件合并为一个文件,它仍然可以像一个连续的 json 文件一样正常流动。

[
    {
        "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. PowerShell 可以在此处本地连接数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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