Skip to content

Create Dataset using Server Script

What is Server Script?

Server-side scripting involves employing scripts on a server to produce a response customized for each user’s request to the website.

Follow the steps to apply the server script on the dataset

  1. Click on the “Edit Dataset” button, then from the top right corner, select “Server Script,” as shown in the figure below:

Image Image

  1. To utilize the server script, check the checkbox, choose the desired language from the dropdown menu, and use the script box on the right to write your code.

Example code below:

import json

g = '${aivfile}'
with open(g, 'r') as file:
    json_data = json.load(file)
    for data in json_data:
        if data['PARTY'] == 'DEMOCRAT':
            data['PARTY'] = 'DECLINED'
output = {'format': 'json', 'data': json_data}
output_json = json.dumps(output, indent=4)
print(output_json)

Short Explanation:

  1. Import the json module to work with JSON data.
  2. Set the file path using the variable g.
  3. Open and read the JSON file into json_data.
  4. Iterate through json_data and change the 'PARTY' value from 'DEMOCRAT' to 'DECLINED'.
  5. Create an output dictionary containing the modified data.
  6. Convert the output dictionary to a JSON string with indentation for readability.
  7. Print the resulting JSON string.

This script modifies the 'PARTY' field in the JSON data and prints the updated JSON.

  1. After adding the code, click on the “Preview” button to check the data preview.

Image

  1. Click on the “Create” button to complete the dataset.