import os
import json

# Folder containing your JSON files
SOURCE_FOLDER = './json_124/'  # Change this path if needed
OUTPUT_FILE = 'merged_nse.json'

merged_data = []

# Iterate over all .json files in the folder
for filename in os.listdir(SOURCE_FOLDER):
    if filename.endswith(".json"):
        file_path = os.path.join(SOURCE_FOLDER, filename)
        with open(file_path, 'r') as f:
            try:
                data = json.load(f)
                merged_data.append(data)
            except json.JSONDecodeError as e:
                print(f"❌ Failed to parse {filename}: {e}")

# Save the merged list to one output file
with open(OUTPUT_FILE, 'w') as f:
    json.dump(merged_data, f, indent=4)

print(f"✅ Merged {len(merged_data)} JSON files into {OUTPUT_FILE}")
