对嵌套对象中的字段进行JQ过滤

问题描述 投票:2回答:1

我有一大堆数据,我使用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"
}

这很好。我现在有对象,每个对象包含parentID 0001,并且数组中的不同项在每个对象中关联。

当我跑:cat tmpData.txt | jq {'id: .id, type: .type, batterID: .batter[].id, batterType: .batter[].type'}

随着添加的type字段,我得到了许多错误关联项目的重复项

{
  "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"
}] 

感谢您的专业知识!

EDIT已解决:工作指令:cat tmpData.txt | jq '[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]'

arrays json bash filtering jq
1个回答
4
投票
  1. “没有逗号”的输出是JSON流;要发出一个数组,请将jq过滤器包装在方括号中。
  2. 你可以将{id: id, type: .type}缩写为{id, type}
  3. 重复.batter []的过滤器具有创建笛卡尔积的效果。你明显想要的只是扩展.batter一次。

把所有东西放在一起:

[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]
© www.soinside.com 2019 - 2024. All rights reserved.