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
- Click on the “Edit Dataset” button, then from the top right corner, select “Server Script,” as shown in the figure below:
- 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:
- Import the
json
module to work with JSON data. - Set the file path using the variable
g
. - Open and read the JSON file into
json_data
. - Iterate through
json_data
and change the'PARTY'
value from'DEMOCRAT'
to'DECLINED'
. - Create an output dictionary containing the modified data.
- Convert the output dictionary to a JSON string with indentation for readability.
- Print the resulting JSON string.
This script modifies the 'PARTY'
field in the JSON data and prints the updated JSON.
- After adding the code, click on the “Preview” button to check the data preview.
- Click on the “Create” button to complete the dataset.