jq 两个对象合并_JQ如何将多个对象合并为一个

Given the following input (which is a toned down version of the output with 100K+ objects of another complex query):

echo '{ "a": { "b":"c", "d":"e" } }{ "a": { "b":"f", "d":"g" } }' | jq '.'

{

"a": {

"b": "c",

"d": "e"

}

}

{

"a": {

"b": "f",

"d": "g"

}

}

desired output:

{

"c": "e",

"f": "g"

}

or (suits better for follow up usage):

{

x: {

"c": "e",

"f": "g"

}

}

I can't for the life of me figure out how to do it. My real problem of course is the multiple object input data, for which I really don't know whether it's valid JSON. Jq produces and accepts it, jshon does not. I tried various possibilities, but none of them produced what I need. I considered this the most likely candidate:

echo '{ "a": { "b":"c", "d":"e" } }{ "a": { "b":"f", "d":"g" } }' | jq ' . | { (.a.b): .a.d }'

{

"c": "e"

}

{

"f": "g"

}

But alas. Other things I tried:

' . | { x: { (.a.b): .a.d } }'

'{ x: {} | . | add }'

'{ x: {} | . | x += }'

'{ x: {} | x += . }'

'x: {} | .x += { (.a.b): .a.d }'

'{ x: {} } | .x += { (.a.b): .a.d }'

Another one, close, but no sigar:

'reduce { (.a.b): .a.d } as $item ({}; . + $item)'

{

"c": "e"

}

{

"f": "g"

}

Who cares to enlighten me?

So the full answer in the above use case, thanks to @peak, is

echo '{ "a": { "b": "c", "d": "e" } }{ "a": { "b": "f", "d": "g" } }' | jq -n '{ x: [inputs | .a | { (.b): .d} ] | add }'

{

"x": {

"c": "e",

"f": "g"

}

}

解决方案

Let's assume you have jq 1.5 or later, and that your JSON objects are in one or more files. Then for your first expected output you can simply write:

[inputs | .a | { (.b): .d} ] | add

This assumes you use the -n command-line option. I'm sure that once you see how simple the solution is, further explanation will be superfluous.

For your second expected output, you can just wrap the above line in:

{x: _}

E.g.:

$ jq -ncf program.jq input.json

{"x":{"c":"e","f":"g"}}

p.s. Your approach using reduce is fine, but you'd need to use the -s command-line option.

p.p.s. Do you owe me another beer?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值