File: merge-json.py

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,235,796 kB
  • sloc: cpp: 7,617,614; ansic: 1,433,901; asm: 1,058,726; python: 252,096; f90: 94,671; objc: 70,753; lisp: 42,813; pascal: 18,401; sh: 10,032; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,401; javascript: 2,272; xml: 892; fortran: 770
file content (47 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
"""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.
"""

import argparse
import json
import sys


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "-o",
        type=str,
        help="The output file to write JSON data to",
        default=None,
        nargs="?",
    )
    parser.add_argument(
        "json_files", type=str, nargs="+", help="Input JSON files to merge"
    )
    args = parser.parse_args()

    merged_data = []

    for json_file in args.json_files:
        try:
            with open(json_file, "r") as f:
                data = json.load(f)
                merged_data.extend(data)
        except (IOError, json.JSONDecodeError) as e:
            continue

    # 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__":
    main()