我有一个包含重复日期的json,我想将重复日期合并到单个json对象中。 数据:
[
{"date":"2017-06-26","mac":"66"},
{"date":"2017-06-26","window":"400"},
{"date":"2017-07-03","mac":"19"},
{"date":"2017-07-03","window":"12"}
]
输出应为:
[
{"date":"2017-06-26","mac":"66","window":"400"},
{"date":"2017-07-03","mac":"19","window":"12"}
]
答案 0 :(得分:0)
这是一个执行此操作的javascript函数,您可以在传递数组并获取新的json后将JSON.stringify(...)应用于输出。
(( a ) => {
// used to check for already inserted dates
let withoutDupes = { };
if(Array.isArray(a)) {
a.forEach( (item) => {
// assuming item has a "date" property inside
if(withoutDupes[item.date]) {
withoutDupes[item.date] = Object.assign( item, withoutDupes[item.date] );
} else {
withoutDupes[item.date] = item;
}
} );
}
return Object.values( withoutDupes );
})( a )
答案 1 :(得分:-1)
您可以尝试使用jq
command line parser及其group_by
功能:
jq '[group_by(.date)|.[]|add]' file
[
{
"date": "2017-06-26",
"mac": "66",
"window": "400"
},
{
"date": "2017-07-03",
"mac": "19",
"window": "12"
}
]