Skip to content

Instantly share code, notes, and snippets.

@fedorn
Last active March 10, 2017 22:35
Show Gist options
  • Select an option

  • Save fedorn/d286c4e6211421ddb35c8c98bb0735fb to your computer and use it in GitHub Desktop.

Select an option

Save fedorn/d286c4e6211421ddb35c8c98bb0735fb to your computer and use it in GitHub Desktop.
Average JSON files
#!/usr/bin/env python3
import sys
import json
import numbers
from collections import defaultdict
json_paths = sys.argv[1:]
all_json = {}
for json_path in json_paths:
with open(json_path) as json_file:
all_json[json_path] = json.load(json_file)
# print(all_json)
result_json = defaultdict(float)
for key in all_json[json_paths[0]]:
if isinstance(all_json[json_paths[0]][key], numbers.Number):
for json_path, json_values in all_json.items():
result_json[key] += json_values[key]
for key in result_json:
result_json[key] /= len(all_json)
print(json.dumps(result_json, sort_keys=True, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment