我有一大组数据,我使用JQ构造一个对象,它只包含我感兴趣的记录数据。我的问题是我开始看到重复的对象,似乎我的语法不正确。
我正在处理一个包含平面域和一个子对象数组的对象,有一些特定的域我想提取出来并创建新的对象,这些对象拥有我想要的所有数据。包括一些平面字段和数组对象中的一些字段。
下面是一个较小的示例,有助于演示问题tmpData.json

{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batter": [{
        "id": "1001",
        "type": "Regular"
    },
    {
        "id": "1002",
        "type": "Chocolate"
    },
    {
        "id": "1003",
        "type": "Blueberry"
    },
    {
        "id": "1004",
        "type": "Devil's Food"
    }
]
}

我运行这个:cat tmpData.txt | jq {'id: .id, type: .type, batter: .batter[].id'}
它输出这个非json对象集(缺少逗号)
{
  "id": "0001",
  "type": "donut",
  "batter": "1001"
}
{
  "id": "0001",
  "type": "donut",
  "batter": "1002"
}
{
  "id": "0001",
  "type": "donut",
  "batter": "1003"
}
{
  "id": "0001",
  "type": "donut",
  "batter": "1004"
}

这很好。现在我有了每个包含parentID0001的对象,并且数组中的不同项与每个对象相关联。
当我运行时:cat tmpData.txt | jq {'id: .id, type: .type, batterID: .batter[].id, batterType: .batter[].type'}
通过addedtype字段,我得到了许多错误关联项的重复项
{
  "id": "0001",
  "type": "donut",
  "batterID": "1001",
  "batterType": "Regular"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1001",
  "batterType": "Chocolate"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1001",
  "batterType": "Blueberry"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1001",
  "batterType": "Devil's Food"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1002",
  "batterType": "Regular"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1002",
  "batterType": "Chocolate"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1002",
  "batterType": "Blueberry"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1002",
  "batterType": "Devil's Food"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1003",
  "batterType": "Regular"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1003",
  "batterType": "Chocolate"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1003",
  "batterType": "Blueberry"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1003",
  "batterType": "Devil's Food"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1004",
  "batterType": "Regular"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1004",
  "batterType": "Chocolate"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1004",
  "batterType": "Blueberry"
}
{
  "id": "0001",
  "type": "donut",
  "batterID": "1004",
  "batterType": "Devil's Food"
}

现在我看到每个batterID都在一个对象中,每个类型都是regular, chocolate, blueberry。但事实上1002只是chocolate而已。
我的理想输出是这样的
 [{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Regular"
},
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Chocolate"
}]

感谢您的专业知识!
编辑已解决:工作命令:cat tmpData.txt | jq '[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]'

最佳答案

输出“不带逗号”是一个JSON流;要发出数组,请将jq过滤器包装在方括号中。
您可以将{id: id, type: .type}缩写为{id, type}
重复.batter[]的过滤器具有创建笛卡尔积的效果。你显然想要什么
相反是扩张。击球一次。
把所有的东西放在一起:

[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]

09-25 16:12
查看更多