You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
I'm trying to compare two json payloads. Essentially I want : diff_json = todays_json - yesterdays_json. It seems like this should be possible, but so far I haven't figured out how to do it. If it is, documentation would be helpful.
The end result should include the full original paths to the changed value, with all full-common keys and values removed.
This seems to work, but I'm not sure it's the best solution.
def diff_json(json1, json2):
differences = DeepDiff(json1, json2, ignore_order=True).to_dict()
def extract_changes(diffs):
result = {}
for change_type in ['values_changed', 'type_changes', 'dictionary_item_added', 'dictionary_item_removed',
'iterable_item_added', 'iterable_item_removed']:
if change_type in diffs:
for key, change in diffs[change_type].items():
path = key.lstrip("root")
if change_type == 'values_changed' or change_type == 'type_changes':
# For value changes or type changes, we show the new value
path = path.replace('[', '.[') # Adjust path for lists
set_nested_value(result, path, change['new_value'])
elif change_type in ['dictionary_item_added', 'iterable_item_added']:
path = path.replace('[', '.[') # Adjust path for lists
set_nested_value(result, path, change['value'])
# For removed items, we skip them as we are only interested in the new state
return result
def set_nested_value(dct, path, value):
keys = re.findall(r'\w+|\[\d+\]', path)
current = dct
for key in keys[:-1]:
if re.match(r'\[\d+\]', key): # If the key is a list index
index = int(key[1:-1])
while len(current) <= index:
current.append({})
current = current[index]
else:
if key not in current:
current[key] = {}
current = current[key]
final_key = keys[-1]
if re.match(r'\[\d+\]', final_key):
index = int(final_key[1:-1])
while len(current) <= index:
current.append({})
current[index] = value
else:
current[final_key] = value
return extract_changes(differences)
output = diff_json(payload2, payload1)
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
I'm trying to compare two json payloads. Essentially I want : diff_json = todays_json - yesterdays_json. It seems like this should be possible, but so far I haven't figured out how to do it. If it is, documentation would be helpful.
The end result should include the full original paths to the changed value, with all full-common keys and values removed.
eg:
Describe the solution you'd like
A clear and concise description of what you want to happen.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: