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