Scipt:ExtractItemsByType
From The Walkscape Walkthrough
import json
import os
# Path to the JSON file
json_file_path = '../json-data/items_official_safe_3-9-2024.json'
# Read the JSON file
with open(json_file_path) as file:
data = json.load(file)
# Create a directory to store the output files
output_dir = '../wiki-json'
os.makedirs(output_dir, exist_ok=True)
print(f'Created directory: {output_dir}')
# Create a dictionary to hold the objects by type
objects_by_type = {}
# Iterate over the objects in the JSON file
for obj in data:
# Extract the value of the "type" key
obj_type = obj.get('type')
# Skip if "type" key is not present in the object
if not obj_type:
Warning(f'Object {obj} does not have a "type" key. Skipping...')
continue
# Add the object to the corresponding list in the dictionary
if obj_type not in objects_by_type:
objects_by_type[obj_type] = []
objects_by_type[obj_type].append(obj)
# Write each list of objects to a separate file
for obj_type, objects in objects_by_type.items():
output_file_path = os.path.join(output_dir, f'{obj_type}.json')
with open(output_file_path, 'w') as output_file:
output_file.write('<syntaxhighlight lang="json">\n')
json.dump(objects, output_file, indent=2)
output_file.write('\n
')
output_file.write('\n')
</syntaxhighlight>