JQ How to merge multiple objects into one

独自空忆成欢 提交于 2019-12-06 07:23:50

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!