2017-08-06 81 views
1

我有两个对象相同的按键:合并两个对象相同的按键

{"key": "first_name", "value": "Jane"} 
{"key": "last_name", "value": "Doe"} 

,我想

​​

我怎样才能在jq做到这一点?

+1

[from_entries](https://stedolan.github.io/jq/manual/#to_entries,from_entries,with_entries)。 –

回答

2

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流入一个大阵列并运行一次过滤器