File size: 1,359 Bytes
5b556c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
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)