Functions | Variables
python.merge_json Namespace Reference

Functions

def merge_json_objects (json_objects)
 
def merge_json_files (json_filenames)
 

Variables

 json_filenames = sys.argv[1:]
 

Function Documentation

def python.merge_json.merge_json_files (   json_filenames)

Definition at line 48 of file merge_json.py.

48 def merge_json_files(json_filenames):
49 
50  # Decode all json files into json objects.
51 
52  json_objects = []
53  for json_filename in json_filenames:
54  json_file = open(json_filename)
55  if not json_file:
56  raise IOError('Unable to open json file %s.' % json_filename)
57  obj = json.load(json_file)
58  json_objects.append(obj)
59 
60  # Combine json objects into a single result object.
61 
62  merged = merge_json_objects(json_objects)
63  print(json.dumps(merged, indent=2, sort_keys=True))
64 
int open(const char *, int)
Opens a file descriptor.
def merge_json_files(json_filenames)
Definition: merge_json.py:48
def merge_json_objects(json_objects)
Definition: merge_json.py:26
def python.merge_json.merge_json_objects (   json_objects)

Definition at line 26 of file merge_json.py.

26 def merge_json_objects(json_objects):
27 
28  merged = {}
29 
30  # Loop over input objects and insert all keys into merged object.
31  # It is an error if the same key is encountered twice unless tha values
32  # are identical.
33 
34  for json_object in json_objects:
35  for key in list(json_object.keys()):
36  if key in list(merged.keys()):
37  if json_object[key] != merged[key]:
38  raise RuntimeError('Duplicate nonmatching key %s.' % key)
39  else:
40  merged[key] = json_object[key]
41 
42  # Done.
43 
44  return merged
45 
46 # Function to merge json files and print the result on standard output.
47 
def merge_json_objects(json_objects)
Definition: merge_json.py:26

Variable Documentation

python.merge_json.json_filenames = sys.argv[1:]

Definition at line 67 of file merge_json.py.