Ferramenta de Mesclagem JSON
Mescla profundamente múltiplos objetos JSON em um com opções de resolução de conflitos.
JSON Object #1
JSON Object #2
Perguntas Frequentes
Implementação de Código
import json
from copy import deepcopy
def deep_merge(base: dict, override: dict, strategy: str = "last_wins") -> dict:
"""
Deep merge two dicts.
strategy: 'last_wins' | 'first_wins' | 'array_concat'
"""
result = deepcopy(base)
for key, value in override.items():
if key in result:
if isinstance(result[key], dict) and isinstance(value, dict):
result[key] = deep_merge(result[key], value, strategy)
elif isinstance(result[key], list) and isinstance(value, list) and strategy == "array_concat":
result[key] = result[key] + value
else:
if strategy != "first_wins":
result[key] = deepcopy(value)
else:
result[key] = deepcopy(value)
return result
# Example
a = {"name": "Alice", "scores": [1, 2], "meta": {"v": 1}}
b = {"name": "Bob", "scores": [3, 4], "meta": {"v": 2, "new": True}}
print(json.dumps(deep_merge(a, b, "last_wins"), indent=2))
print(json.dumps(deep_merge(a, b, "first_wins"), indent=2))
print(json.dumps(deep_merge(a, b, "array_concat"),indent=2))
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.