使用PowerShell合并/串联多个json文件 [英] Merge/Concatenate multiple json files using PowerShell

查看:170
本文介绍了使用PowerShell合并/串联多个json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道我可以在Powershell中使用以下语法来连接两个json文件.

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

获取内容"C:\ File1.json","C:\ File2.json" |设置内容"C:\ CombinedOutput.json"

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

但是,当我执行此脚本时,我几乎得到了我所需要的东西,除了左首和尾括号(一个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.

,

然后,所需的输出文件将如下所示,甚至将几个json文件合并为一个文件,仍然可以使其像一个连续的json文件一样正确地流动.

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

PowerShell can concatenate arrays natively here.

这篇关于使用PowerShell合并/串联多个json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆