I have a large set of data, I am using JQ to construct the object that contains only the data I am interested in for a record. My problem is that I am starting to see duplicate objects, it seems my syntax is incorrect.
我有一组大的数据,我正在使用JQ构造一个对象,该对象只包含我对记录感兴趣的数据。我的问题是我开始看到重复的对象,看起来我的语法不正确。
I am working with an object that contains flat fields and an array of subObjects, there are particular fields I want to pull out and make new objects that have all the data I want. Including some flat fields and some fields from the array objects.
我正在处理一个对象,该对象包含平坦的字段和一个子对象数组,其中有一些特定的字段,我想要取出并创建具有所有我想要的数据的新对象。包括一些平面字段和来自数组对象的字段。
Here is a smaller sample that helps demonstrate problem tmpData.json
下面是一个较小的示例,它有助于演示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"
}
]
}
I run this : cat tmpData.txt | jq {'id: .id, type: .type, batter: .batter[].id'}
我运行这个:cat tmpData。jq {'id: .id,类型:.type,打击:.打击:.打击[].id'}
Which outputs this non-json set of objects (it's missing commas)
输出非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"
}
This is good. I now have objects each containing the parentID 0001
and the different items in the array are associated in each object.
这是很好的。现在,我有了每个包含parentID 0001的对象,并且数组中的不同项在每个对象中都是关联的。
When I run: cat tmpData.txt | jq {'id: .id, type: .type, batterID: .batter[].id, batterType: .batter[].type'}
运行时:cat tmpData。txt | jq {'id: .id, type: .type, batterID: .面糊[]id,batterType:。bat[].type ' }
With the added type
field I get a lot of duplicates that wrongly associate items
在添加的类型字段中,我得到了许多重复错误的关联项。
{
"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"
}
Now I see that each batterID
is in an object with every type regular, chocolate, blueberry
. But in fact 1002
is only ever chocolate
.
现在我看到每个面糊都在一个物体上有各种普通的,巧克力,蓝莓。但事实上1002只是巧克力。
My ideal output would be like this
我的理想输出是这样的
[{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Regular"
},
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Chocolate"
}]
Your expertise is appreciated!
你的专长是赞赏!
EDIT SOLVED: working command: cat tmpData.txt | jq '[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]'
编辑已解决:工作命令:cat tmpData。jq '[{id, type} +(。面糊[]|{面糊id: .id,面糊类型:.type}]'
1 个解决方案
#1
4
- The output "without commas" is a stream of JSON; to emit an array, wrap your jq filter in square brackets.
- 输出“无逗号”是JSON流;要发出数组,请将jq过滤器括在方括号中。
- You can abbreviate
{id: id, type: .type}
to{id, type}
- 可以将{id: id, type: .type}缩写为{id, type}
- Your filter which repeats .batter[] has the effect of creating a Cartesian product. What you evidently want instead is to expand .batter just once.
- 重复。面糊[]的过滤器具有创建笛卡尔积的效果。显然你想要的是扩大一次。
Putting everything together:
把一切放在一起:
[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]
#1
4
- The output "without commas" is a stream of JSON; to emit an array, wrap your jq filter in square brackets.
- 输出“无逗号”是JSON流;要发出数组,请将jq过滤器括在方括号中。
- You can abbreviate
{id: id, type: .type}
to{id, type}
- 可以将{id: id, type: .type}缩写为{id, type}
- Your filter which repeats .batter[] has the effect of creating a Cartesian product. What you evidently want instead is to expand .batter just once.
- 重复。面糊[]的过滤器具有创建笛卡尔积的效果。显然你想要的是扩大一次。
Putting everything together:
把一切放在一起:
[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]