-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_to_text.py
More file actions
30 lines (25 loc) · 974 Bytes
/
json_to_text.py
File metadata and controls
30 lines (25 loc) · 974 Bytes
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
# json_to_text.py
import json
def json_to_txt(json_data, output_file):
# Open the output text file
with open(output_file, 'w') as file:
# Recursive function to process JSON data and write to file
def process_json(data, indent=0):
# For dictionary type data
if isinstance(data, dict):
for key, value in data.items():
file.write(' ' * indent + f"{key}:\n")
process_json(value, indent + 1)
# For list type data
elif isinstance(data, list):
for item in data:
process_json(item, indent)
# For other data types (strings, numbers)
else:
file.write(' ' * indent + str(data) + '\n')
process_json(json_data)
# Example usage
if __name__ == "__main__":
with open('output.json', 'r') as f:
json_data = json.load(f)
json_to_txt(json_data, 'output.txt')