Advertisement
Python253

create_example_json

Mar 11th, 2024
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: create_example_json.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script creates an example JSON file ('example.json') with sample data.
  10.  
  11. Requirements:
  12. - Python 3.x
  13.  
  14. Usage:
  15. 1. Save this script as 'create_example_json.py'.
  16. 2. Run the script.
  17.  
  18. Note: Adjust the 'json_filename' variable in the script as needed.
  19. """
  20.  
  21. import json
  22.  
  23. def create_example_json(json_filename):
  24.     data = {
  25.         "name": "John Doe",
  26.         "age": 30,
  27.         "city": "New York",
  28.         "interests": ["reading", "traveling", "coding"]
  29.     }
  30.  
  31.     with open(json_filename, 'w') as jsonfile:
  32.         json.dump(data, jsonfile, indent=2)
  33.  
  34. if __name__ == "__main__":
  35.     # Set the filename for the example JSON file
  36.     json_filename = 'example.json'
  37.    
  38.     # Create the example JSON file in the current working directory
  39.     create_example_json(json_filename)
  40.  
  41.     print(f"Example JSON file '{json_filename}' created in the current working directory.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement