动态合并两个JSON对象的技术实现需求
Got it, let's work through merging these two JSON objects dynamically. Looking at your data, the only key difference between json1 and json2 is the careerLevel field—everything else has matching values. The goal here is to combine them into a clean, structured result that groups entries by their career level while preserving all the monthly and career-specific data.
Core Merging Logic
We'll structure the merged result to:
- Include a top-level summary of overall monthly totals (since both objects share identical values here)
- Group each original JSON entry by its
careerLevel, keeping individual monthly data and career details intact - Keep the solution dynamic so it works even if you add more similar JSON objects later
JavaScript Implementation
Here's a reusable function to handle the merge:
const json1 = { '201801': 58, '201802': 74, careerLevel: 'Analyst', careerLevels: [{ '201801': 29, '201802': 37, careerID: '10000100' }, { '201801': 29, '201802': 37, careerID: '10000110' } ] }; const json2 = { '201801': 58, '201802': 74, careerLevel: 'Consultant', careerLevels: [{ '201801': 29, '201802': 37, careerID: '10000100' }, { '201801': 29, '201802': 37, careerID: '10000110' } ] }; // Dynamic merge function function mergeCareerJSONs(jsonList) { const mergedResult = { summary: { '201801': jsonList[0]['201801'], '201802': jsonList[0]['201802'] }, careerGroups: jsonList.map(jsonObj => ({ careerLevel: jsonObj.careerLevel, monthlyData: { '201801': jsonObj['201801'], '201802': jsonObj['201802'] }, careerDetails: jsonObj.careerLevels })) }; return mergedResult; } // Execute merge and log result const mergedJSON = mergeCareerJSONs([json1, json2]); console.log(JSON.stringify(mergedJSON, null, 2));
Output
{ "summary": { "201801": 58, "201802": 74 }, "careerGroups": [ { "careerLevel": "Analyst", "monthlyData": { "201801": 58, "201802": 74 }, "careerDetails": [ { "201801": 29, "201802": 37, "careerID": "10000100" }, { "201801": 29, "201802": 37, "careerID": "10000110" } ] }, { "careerLevel": "Consultant", "monthlyData": { "201801": 58, "201802": 74 }, "careerDetails": [ { "201801": 29, "201802": 37, "careerID": "10000100" }, { "201801": 29, "201802": 37, "careerID": "10000110" } ] } ] }
Python Implementation
If you're working in Python, here's an equivalent solution:
import json json1 = { '201801': 58, '201802': 74, 'careerLevel': 'Analyst', 'careerLevels': [{ '201801': 29, '201802': 37, 'careerID': '10000100' }, { '201801': 29, '201802': 37, 'careerID': '10000110' } ] } json2 = { '201801': 58, '201802': 74, 'careerLevel': 'Consultant', 'careerLevels': [{ '201801': 29, '201802': 37, 'careerID': '10000100' }, { '201801': 29, '201802': 37, 'careerID': '10000110' } ] } def merge_career_jsons(json_list): merged_result = { "summary": { "201801": json_list[0]["201801"], "201802": json_list[0]["201802"] }, "careerGroups": [] } for json_obj in json_list: career_group = { "careerLevel": json_obj["careerLevel"], "monthlyData": { "201801": json_obj["201801"], "201802": json_obj["201802"] }, "careerDetails": json_obj["careerLevels"] } merged_result["careerGroups"].append(career_group) return merged_result # Execute merge and print result merged_json = merge_career_jsons([json1, json2]) print(json.dumps(merged_json, indent=2))
Extension: Merge Duplicate Career IDs
If you ever have duplicate careerID entries across your JSON objects and want to sum their monthly values, you can adjust the logic. For example, in JavaScript:
// Updated careerDetails handling to merge duplicate careerIDs careerDetails: jsonList.flatMap(json => json.careerLevels) .reduce((acc, curr) => { const existing = acc.find(item => item.careerID === curr.careerID); if (existing) { existing['201801'] += curr['201801']; existing['201802'] += curr['201802']; } else { acc.push({...curr}); } return acc; }, [])
This will combine all entries with the same careerID and sum their monthly metrics.
内容的提问来源于stack exchange,提问作者rp4361

