MCPcopy Create free account
hub / github.com/nlweb-ai/NLWeb / merge_json_objects

Function merge_json_objects

AskAgent/python/core/utils/json_utils.py:107–135  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

105# ============= JSON Merging Functions =============
106
107def 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
138def _merge_dicts(dict1: dict[str, Any], dict2: dict[str, Any]) -> dict[str, Any]:

Callers 2

merge_json_arrayFunction · 0.85
test_mergeFunction · 0.85

Calls 2

_merge_dictsFunction · 0.85
jsonifyFunction · 0.70

Tested by 1

test_mergeFunction · 0.68