import json # Specify the path to your JSON file file_path = 'github_dataset.json' def update_custom_key_in_json(file_path): try: # Open the JSON file and load the data with open(file_path, 'r') as file: data = json.load(file) # Check if the data is a list of dictionaries if isinstance(data, list): for item in data: if isinstance(item, dict) and 'output' in item: # Get the 'output' value and split it into words output_text = item['output'] item['instruction'] = 'you are the github assistant' words = output_text.split() # Truncate the words to 2048 words if needed if len(words) > 2048: item['output'] = ' '.join(words[:2048]) # Save the updated data back to the JSON file with open(file_path, 'w') as file: json.dump(data, file, indent=4) print("Output truncated to 2048 words successfully!") else: print("The JSON file doesn't contain a list of dictionaries.") except Exception as e: print(f"An error occurred: {e}") # Call the function to update the custom key update_custom_key_in_json(file_path)