1
我有两个对象相同的按键:合并两个对象相同的按键
{"key": "first_name", "value": "Jane"}
{"key": "last_name", "value": "Doe"}
,我想
我怎样才能在jq
做到这一点?
我有两个对象相同的按键:合并两个对象相同的按键
{"key": "first_name", "value": "Jane"}
{"key": "last_name", "value": "Doe"}
,我想
我怎样才能在jq
做到这一点?
JQ溶液中取出答案(使用reduce
功能):
jq -s 'reduce .[] as $o ({}; .[$o.key]=$o.value)' yourfile
或者使用from_entries
功能更简单:
jq -s 'from_entries' yourfile
输出(两种方法):
{
"first_name": "Jane",
"last_name": "Doe"
}
--slurp/-s
- 选项,读取整个inpu t流入一个大阵列并运行一次过滤器下面应该为你
jq 'map({(.key): .value}) | add' test.json
输出
{
"first_name": "Jane",
"last_name": "Doe"
}
[from_entries](https://stedolan.github.io/jq/manual/#to_entries,from_entries,with_entries)。 –