merge_jsons.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # coding=utf-8
  2. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import glob
  16. import sys
  17. import json
  18. import argparse
  19. if __name__ == '__main__':
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument("--json_path", type=str, default=".",
  22. help="path where all the json files are located")
  23. parser.add_argument("--output_file", type=str, default="merged_output.json",
  24. help="filename where the merged json should go")
  25. args = parser.parse_args()
  26. json_path = args.json_path
  27. out_file = args.output_file
  28. json_files = glob.glob(json_path + '/*.json')
  29. counter = 0
  30. with open(out_file, 'w') as outfile:
  31. for fname in json_files:
  32. counter += 1
  33. if counter % 1024 == 0:
  34. print("Merging at ", counter, flush=True)
  35. with open(fname, 'r') as infile:
  36. for row in infile:
  37. each_row = json.loads(row)
  38. outfile.write(row)
  39. print("Merged file", out_file, flush=True)