Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 按类型拆分大JSON文件_Powershell - Fatal编程技术网

Powershell 按类型拆分大JSON文件

Powershell 按类型拆分大JSON文件,powershell,Powershell,考虑这种格式的大JSON(例如:all.JSON): 我想按类型将此文件拆分为两个文件 series.json [ { "Name": "bcd", "Type": "series" }, { "Name": "sdf", "Type": "series&qu

考虑这种格式的大JSON(例如:all.JSON):

我想按类型将此文件拆分为两个文件

series.json

 [
    {
        "Name": "bcd",
        "Type": "series"
    },
    {
        "Name": "sdf",
        "Type": "series"
    }
]
movie.json

 [
    {
        "Name": "abc",
        "Type": "movie"
    },
        {
        "Name": "asd",
        "Type": "movie"
    }
 ]
使用powershell进行此拆分的更好方法是什么?有人可以帮忙吗?

试试这个:

s = []
d = []
with open('casts.json', 'r', encoding='utf8', newline='') as file:
    reader = json.load(file)
    print(reader)
    for i in reader:
        if i['Type'] == 'movie':
            s.append(i)
        else:
            d.append(i)
print(d)
print(s)
with open('series.json', 'w', encoding='utf8') as file:
    json.dump(d, file, ensure_ascii=False)
with open('movies.json', 'w', encoding='utf8') as file:
    json.dump(s, file, ensure_ascii=False)
#import data from file
$Array=Get-Content "C:\temp\test.json" | ConvertFrom-Json

#group data by type and export
$Array | group Type | %{

$File="C:\temp\{0}.json" -f $_.Name
$_.Group | ConvertTo-Json | Out-File $File
}

看起来不像PowerShell欢迎来到StackOverflow。请告诉我们你自己试过什么。注意,这不是一个脚本工厂。您需要自己编写脚本。如果您遇到现有问题和答案尚未涵盖的问题,您可以在此处发布您的答案,以便我们能够进一步帮助您。另见:谢谢,正是我想要的。。
#import data from file
$Array=Get-Content "C:\temp\test.json" | ConvertFrom-Json

#group data by type and export
$Array | group Type | %{

$File="C:\temp\{0}.json" -f $_.Name
$_.Group | ConvertTo-Json | Out-File $File
}