How to Split Large Automation File into Small Files

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.

Disclaimer

Atlassian does not support the code below, which is provided "AS IS." This article provides a piece of code that illustrates one way to achieve the desired goal.

Feedback provided at the bottom of the page is appreciated, but won't be handled as support.

Summary

You need to import a JSON file containing several automation rules and you face the following error:


Environment

  • Jira Cloud
  • This script requires Python 3 to be installed


Solution

  • The code below assumes you have a json file called "automation-rules.json" with your original rules exported
  • The code will create a new folder called "output_batches", where the splited files will be placed
  • The code is set to split the files in batches of 200 rules. This can be changed 

Split_rules
import json
import os

def split_json_file(file_path, output_dir, batch_size):
    # Ensure output directory exists
    os.makedirs(output_dir, exist_ok=True)

    # Load the main JSON file
    with open(file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # Check if it's the expected structure
    if 'rules' not in data:
        print("Invalid JSON format: 'rules' key not found.")
        return

    # Get the list of rules
    rules = data['rules']
    total_rules = len(rules)
    
    # Split rules into batches
    for i in range(0, total_rules, batch_size):
        # Slice the list of rules for the current batch
        batch_rules = rules[i:i + batch_size]
        batch_data = {"rules": batch_rules}

        # Define the output file path
        batch_file_name = f"batch_{i // batch_size + 1}.json"
        batch_file_path = os.path.join(output_dir, batch_file_name)

        # Write the batch to a new JSON file
        with open(batch_file_path, 'w', encoding='utf-8') as f:
            json.dump(batch_data, f, indent=4)

        print(f"Created batch file: {batch_file_path} with {len(batch_rules)} rules")

# Usage example
file_path = 'automation-rules.json'  # Path to the large JSON file
output_dir = 'output_batches'  # Directory to save the smaller files
batch_size = 200  # Number of rules per batch

split_json_file(file_path, output_dir, batch_size)






Last modified on Nov 12, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.