Merge two JSON objects according to specific rules. Args: obj1: First JSON object (can be string, dict, or list of dicts) obj2: Second JSON object (can be string, dict, or list of dicts) Returns: Merged dictionary combining data from both objects
(obj1: Union[str, dict, list], obj2: Union[str, dict, list])
| 105 | # ============= JSON Merging Functions ============= |
| 106 | |
| 107 | def merge_json_objects(obj1: Union[str, dict, list], obj2: Union[str, dict, list]) -> dict[str, Any]: |
| 108 | """ |
| 109 | Merge two JSON objects according to specific rules. |
| 110 | |
| 111 | Args: |
| 112 | obj1: First JSON object (can be string, dict, or list of dicts) |
| 113 | obj2: Second JSON object (can be string, dict, or list of dicts) |
| 114 | |
| 115 | Returns: |
| 116 | Merged dictionary combining data from both objects |
| 117 | """ |
| 118 | # Parse strings to JSON if needed |
| 119 | obj1 = jsonify(obj1) |
| 120 | obj2 = jsonify(obj2) |
| 121 | |
| 122 | # Extract first dict if either is an array |
| 123 | if isinstance(obj1, list): |
| 124 | obj1 = obj1[0] if obj1 else {} |
| 125 | if isinstance(obj2, list): |
| 126 | obj2 = obj2[0] if obj2 else {} |
| 127 | |
| 128 | # Ensure both are dicts |
| 129 | if not isinstance(obj1, dict): |
| 130 | obj1 = {} |
| 131 | if not isinstance(obj2, dict): |
| 132 | obj2 = {} |
| 133 | |
| 134 | # Merge the dictionaries |
| 135 | return _merge_dicts(obj1, obj2) |
| 136 | |
| 137 | |
| 138 | def _merge_dicts(dict1: dict[str, Any], dict2: dict[str, Any]) -> dict[str, Any]: |