我想合併兩個json文件,第二個json中的匹配stop_id
作爲子文檔嵌套在第一個json中。將json合併到另一個json中作爲子文檔
這就是我的意思是:
first.json:這是一個包含站JSON文件停止的列車,例如。每個都有一個stop_id
字段。
[
{
"stop_id":70021,
"stop_name":"CALTRAIN - 22ND ST STATION",
"stop_lat":37.757692,
"stop_lon":-122.392318,
"zone_id":3329
},
{
"stop_id":70022,
"stop_name":"CALTRAIN - 22ND ST STATION",
"stop_lat":37.757692,
"stop_lon":-122.392318,
"zone_id":3329
},
{
"stop_id":70151,
"stop_name":"CALTRAIN - ATHERTON STATION",
"stop_lat":37.464458,
"stop_lon":-122.198152,
"zone_id":3331
}]
second.json:這包含與一站行程信息,通過stop_id
[
{
"trip_id":"RTD8997283",
"arrival_time":"05:40:00",
"departure_time":"05:40:00",
"stop_id":70021,
"stop_sequence":1
},
{
"trip_id":"RTD8997283",
"arrival_time":"05:52:00",
"departure_time":"05:52:00",
"stop_id":70021,
"stop_sequence":2
},
{
"trip_id":"RTD8449096",
"arrival_time":"07:33:00",
"departure_time":"07:33:00",
"stop_id":70022,
"stop_sequence":1
}]
我想要的方式,合併這些文件,在第二匹配stop_id
秒。 json嵌套在first.json中的stop_id
文檔下面。因此,例如,合併的最終結果是這樣的:
merged.json
[{
"stop_id": 70021,
"stop_name": "CALTRAIN - 22ND ST STATION",
"stop_lat": 37.757692,
"stop_lon": -122.392318,
"zone_id": 3329,
"trip": [{
"trip_id": "RTD8997283",
"arrival_time": "05:40:00",
"departure_time": "05:40:00",
"stop_id": 70021,
"stop_sequence": 1
}, {
"trip_id": "RTD8997283",
"arrival_time": "05:52:00",
"departure_time": "05:52:00",
"stop_id": 70021,
"stop_sequence": 2
}]
}, {
"stop_id": 70022,
"stop_name": "CALTRAIN - 22ND ST STATION",
"stop_lat": 37.757692,
"stop_lon": -122.392318,
"zone_id": 3329,
"trip": [{
"trip_id": "RTD8449096",
"arrival_time": "07:33:00",
"departure_time": "07:33:00",
"stop_id": 70022,
"stop_sequence": 1
}]
}, {
"stop_id": 70151,
"stop_name": "CALTRAIN - ATHERTON STATION",
"stop_lat": 37.464458,
"stop_lon": -122.198152,
"zone_id": 3331
}]
任何方式通過JavaScript或任何其他方式做這樣的合併,對於大型數據集?
如果數據集很大,則可能需要先將對象數組轉換爲一個映射(stopid - > object)用於更快速查找。同樣對於旅行 – gurvinder372
你在服務器或客戶端上工作嗎? – Yoda
您是否特意在javascript中執行此操作,或者像'jq'命令行工具一樣適合您的目的? –