| """A command line utility to merge two JSON files. |
| This is a python program that merges two JSON files into a single one. The |
| intended use for this is to combine generated 'compile_commands.json' files |
| created by CMake when performing an LLVM runtime build. |
| parser = argparse.ArgumentParser(description=__doc__) |
| help="The output file to write JSON data to", |
| "json_files", type=str, nargs="+", help="Input JSON files to merge" |
| args = parser.parse_args() |
| for json_file in args.json_files: |
| with open(json_file, "r") as f: |
| except (IOError, json.JSONDecodeError) as e: |
| # Deduplicate by converting each entry to a tuple of sorted key-value pairs |
| unique_data = list({json.dumps(entry, sort_keys=True) for entry in merged_data}) |
| unique_data = [json.loads(entry) for entry in unique_data] |
| with open(args.o, "w") as f: |
| json.dump(unique_data, f, indent=2) |
| if __name__ == "__main__": |