我有一个很大的文件叫

Metadata_01.json

它由以下 struct 块组成:

[
 {
  "Participant_id": "P04_00001",
  "no_of_people": "Multiple",
  "apparent_gender": "F",
  "geographic_location": "AUS",
  "ethnicity": "Caucasian",
  "capture_device_used": "iOS 14",
  "camera_orientation": "Portrait",
  "camera_position": "Side View",
  "indoor_outdoor_env": "Indoors",
  "lighting_condition": "Bright",
  "Occluded": 1,
  "category": "Two Person",
  "camera_movement": "Still",
  "action": "No action",
  "indoor_outdoor_in_moving_car_or_train": "Indoor",
  "daytime_nighttime": "Nighttime"
 },
 {
  "Participant_id": "P04_00002",
  "no_of_people": "Single",
  "apparent_gender": "M",
  "geographic_location": "AUS",
  "ethnicity": "Caucasian",
  "capture_device_used": "iOS 14",
  "camera_orientation": "Portrait",
  "camera_position": "Frontal View",
  "indoor_outdoor_env": "Outdoors",
  "lighting_condition": "Bright",
  "Occluded": "None",
  "category": "Animals",
  "camera_movement": "Still",
  "action": "Small action",
  "indoor_outdoor_in_moving_car_or_train": "Outdoor",
  "daytime_nighttime": "Daytime"
 },

以此类推.数以千计.

我正在使用以下命令:

jq -cr '.[]' Metadata_01.json | awk '{print > (NR ".json")}'

它在某种程度上完成了预期的工作.

From large file that is structured like this

I am getting tons of files that named like this

And structure like this (in one line)

我需要用"Participant_id"来命名每个json文件,而不是这些结果(例如,p04_00002.json) 并且我希望保留json struct ,使其与每个文件类似

{
  "Participant_id": "P04_00002",
  "no_of_people": "Single",
  "apparent_gender": "M",
  "geographic_location": "AUS",
  "ethnicity": "Caucasian",
  "capture_device_used": "iOS 14",
  "camera_orientation": "Portrait",
  "camera_position": "Frontal View",
  "indoor_outdoor_env": "Outdoors",
  "lighting_condition": "Bright",
  "Occluded": "None",
  "category": "Animals",
  "camera_movement": "Still",
  "action": "Small action",
  "indoor_outdoor_in_moving_car_or_train": "Outdoor",
  "daytime_nighttime": "Daytime"
 }

我应该对上面的命令进行哪些调整才能实现这一点? 或者也许有一个更简单的方法来做到这一点?谢谢!

推荐答案

我建议使用PowerShell,因为总体上使用对象往往更容易.幸运的是,PowerShell有一个ConvertFrom-Jsoncmdlet,您可以使用它将返回的文本转换为PS对象,让您通过点符号(.Participant_id)引用属性.然后,您只需将每个迭代转换回JSON格式并将其导出.在这里,我使用New-Item来创建带有输出的文件,但是通过管道连接到Out-File也可以.

$json = Get-Content -Path '.\Metadata_01.json' -Raw | ConvertFrom-Json 
foreach ($json_object in $json)
{
    New-Item -Path ".\Desktop\" -Name "$($json_object.Participant_id).json" -Value (ConvertTo-Json -InputObject $json_object) -ItemType 'File' -Force
}

我可以看到您probably遇到的问题是内存不足,这是由于该文件的大小,因为在本例中,您将首先保存到一个变量.有一些方法可以绕过它,但这是出于演示目的.

Json相关问答推荐

在Go中,当字段可以根据其他字段具有不同的类型时,什么是正确的方法来卸载JSON?

如何创建可由Gin序列化到json的排序键值映射?

由于无效的UTF-8开始字节0xa0,JSON被拒绝,但编码似乎有效

Ansible - 将文件内容添加到字典中

Jolt 转换数组对象并将某些字段移动到嵌套数组

使用jq根据对象中键的值查找对象

如何在JQ中展平多维数组

使用 TypeScript 接口时如何修复未定义错误?

JOLT JSON 将值从一对多转换为一对一

shell解析json并循环输出组合变量

根据数据框中的其他列值将列表 json 对象插入行

如何一次加载无限滚动中的所有条目以解析python中的HTML

为什么我不能在 C# 中引用 System.Runtime.Serialization.Json

Django - 异常处理最佳实践和发送自定义错误消息

Spring MVC控制器中的JSON参数

在 Rails 3 中处理 JS/ERB 模板中的 JSON

如何在已声明的 JSON 对象中添加键值对

如何按键查找特定的 JSON 值?

Gson 将一组数据对象转换为 json - Android

Microsoft.Net.Http 与 Microsoft.AspNet.WebApi.Client