Advertisement
Python253

json2html

Mar 14th, 2024
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: json2html.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a JSON file (.json) to an HTML file (.html).
  10. It reads the JSON data from the file and generates an HTML representation.
  11.  
  12. Requirements:
  13. - Python 3.x
  14.  
  15. Usage:
  16. 1. Save this script as 'json2html.py'.
  17. 2. Ensure your JSON file ('example.json') is in the same directory as the script.
  18. 3. Run the script.
  19. 4. The converted HTML file ('json2html.html') will be generated in the same directory.
  20.  
  21. Note: Adjust the 'json_filename' and 'html_filename' variables in the script as needed.
  22. """
  23.  
  24. import json
  25.  
  26. def json_to_html(json_filename, html_filename):
  27.     with open(json_filename, 'r') as jsonfile:
  28.         data = json.load(jsonfile)
  29.  
  30.     html_content = "<html><body><pre>\n"
  31.     html_content += json.dumps(data, indent=2, ensure_ascii=False)
  32.     html_content += "\n</pre></body></html>"
  33.  
  34.     with open(html_filename, 'w') as htmlfile:
  35.         htmlfile.write(html_content)
  36.  
  37. if __name__ == "__main__":
  38.     # Set the filenames for the JSON and HTML files
  39.     json_filename = 'example.json'
  40.     html_filename = 'json2html.html'
  41.  
  42.     # Convert the JSON file to an HTML file
  43.     json_to_html(json_filename, html_filename)
  44.  
  45.     print(f"Converted '{json_filename}' to '{html_filename}'.")
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement