Python how to create multiple new UML files using JSON data?

Viewed 23

I have a program that loads multiple JSON files and using the JSON data it generates a UML diagram. Till now I was manually giving the UML file name but now I have multiple JSON files and using each of the JSON data I want to generate UML diagrams and the name of the UML file should be same as JSON file name.

For Example:- There are two JSON file file1.json, files2.json ---> code runs and generates uml diagram with the names file1.uml and file2.uml

Here is the code I have, that takes single json file and generates UML diagram for that file.

with open ("file1.json") as f:
  json_data = json.load(f)

  # From JSON, create GANTT Diagram
with open("file1.uml", "w") as f:
  # Note: uncomment both 2 lines below if want to start the project XX weeks from today ago.
  # today = datetime.datetime.now().date()
  # start_project_date = today + datetime.timedelta(weeks = -8)
  f.write("@startuml\nprintscale weekly\nProject starts 2022-08-01\n")
  # Extract information for Gantt chart
  for stuff in json_data["releases"]:
      new_line = "[Release " + stuff["release"] + "] starts " + stuff["start_date"] + " and 
      ends " + stuff["end_date"] + "\n"
      f.write(new_line)
      print(new_line)
  f.write("@enduml")
#using the uml file i am generating png file using plantuml software
os.system("java -jar test/plantuml.jar test/UML/file1.uml -o ../PNG/")

To load multiple Json file i have this code in place:

json_files = glob.glob('JSON/*.json', recursive=True)

for single_file in json_files:
  with open(single_file, 'r') as f:
    json_data = json.load(f)

But I am not sure how to create multiple files and give them the name as JSON file names. Can someone guide me here? Thanks in advance

1 Answers
json_files = glob.glob('JSON/*.json', recursive=True)

for single_file in json_files:
    with open(single_file, 'r') as f:
        json_data = json.load(f)

    # From JSON, create GANTT Diagram
    path_uml_file = single_file[:-4] + ".uml"
    with open(path_uml_file, "w") as f:
        # Note: uncomment both 2 lines below if want to start the project XX weeks from today ago.
        # today = datetime.datetime.now().date()
        # start_project_date = today + datetime.timedelta(weeks = -8)
        f.write("@startuml\nprintscale weekly\nProject starts 2022-08-01\n")
        # Extract information for Gantt chart
        for stuff in json_data["releases"]:
            new_line = "[Release " + stuff["release"] + "] starts " + stuff["start_date"] + " and ends " + stuff["end_date"] + "\n"
            f.write(new_line)
            print(new_line)
        f.write("@enduml")
    # Run plantuml with the path of the file we just saved
    os.system("java -jar test/plantuml.jar {} -o ../PNG/".format(path_uml_file))

The first loop iterates over the files in the folder "JSON/", then for each filename, run the previous code part.

Then, I save under the same name, except that I replace "json" by "uml" in the end of the file path

Related