make a python script that will enumerate a .json file and take in the "studentnumber"…
- User
make a python script that will enumerate a .json file and take in the "student_number" and "holder_name" fields and put them into their own new json object. make sure every "holder_name" is evalulated with a regex expression that swaps the string to the following: Golden, Devin James will turn into Devin Golden an example json object that will be found in the .json file will be: { "id":"5395834", "deviceid":"5395823", "studentid":"15666", "abbreviation":"WRHS", "student_number":"775004705", "student_dcid":"15575", "users_dcid":"", "teacher_dcid":"", "holder_name":"Fish, Matthew Ryan", "end_of_ownership":"", "start_of_ownership":"10/03/2021", "policy_start_date":"", "policy_end_date":"", "insurance_start_date":"", "insurance_end_date":"", "notes":"", "usage_policy_name":"", "insurance_policy_name":"", "insurance_cost":"", "policy_signed": "0", "insurance_signed": "0" },
- Assistant
Here's a Python script that reads a JSON file, extracts the "student_number" and "holder_name" fields, applies the regex expression to swap the strings, and creates a new JSON object:
import json import re def swap_name(name): regex = r"(\w+), (\w+)" match = re.match(regex, name) if match: return f"{match.group(2)} {match.group(1)}" else: return name def process_json(json_file): with open(json_file) as file: data = json.load(file) output = [] for obj in data: student_number = obj.get("student_number") holder_name = obj.get("holder_name") new_obj = { "student_number": student_number, "holder_name": swap_name(holder_name) } output.append(new_obj) with open("output.json", "w") as file: json.dump(output, file) json_file = "input.json" # Replace with the path to your input JSON file process_json(json_file)Make sure to replace "input.json" in the
json_filevariable with the actual path to your JSON file. The output JSON objects will be written to a file named "output.json". - User
edit the above script to make it evaluate a txt file and allow it to match the emails from that text file to names from the holder_name field from output.json
the txt file will look like this: <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
For example: holder_name:"Abigail Martin" would match with <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
holder_name:"Abigail Bouley" would match with <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> and so on so on.
- Assistant
To edit the script to match emails from a text file to names from the "holder_name" field in
output.json, you can use the following modified code:import json import re def swap_name(name): regex = r"(\w+), (\w+)" match = re.match(regex, name) if match: return f"{match.group(2)} {match.group(1)}" else: return name def process_json(json_file, txt_file): with open(json_file) as file: data = json.load(file) with open(txt_file) as file: emails = file.read().splitlines() output = [] for obj in data: holder_name = obj.get("holder_name") email_match = next((email for email in emails if holder_name.lower() in email.lower()), None) new_obj = { "holder_name": swap_name(holder_name), "email": email_match } output.append(new_obj) with open("output.json", "w") as file: json.dump(output, file) json_file = "output.json" # Replace with the path to your input JSON file txt_file = "emails.txt" # Replace with the path to your input text file process_json(json_file, txt_file)