1 Answer
- Newest
- Most votes
- Most comments
0
Hi raviv,
please go through the below script and steps once i hope it will helps to resolve your issue.
Python Script for Masking Sensitive Data in JSON
import re
import json
# Sample JSON payload
json_payload = """
{
"contactDetails": {
"firstName": "Peter Alpha",
"companyName": "OFN Test",
"email": "test@ab.com",
"phoneNumber": "0000000000"
}
}
"""
# Convert JSON string to a Python dictionary
data = json.loads(json_payload)
# Regular expressions for matching sensitive data
email_regex = r"[^@]+@[^@]+\.[^@]+"
phone_regex = r"\b\d{10}\b"
name_regex = r"\b[A-Z][a-z]*\s[A-Z][a-z]*\b" # Example for FirstName LastName
# Function to mask sensitive data
def mask_data(data):
if isinstance(data, dict):
for key, value in data.items():
if isinstance(value, str):
if re.match(email_regex, value):
data[key] = "*****@*****.com"
elif re.match(phone_regex, value):
data[key] = "**********"
elif re.match(name_regex, value):
data[key] = "***** *****"
else:
mask_data(value)
elif isinstance(data, list):
for item in data:
mask_data(item)
# Mask the data
mask_data(data)
# Convert the dictionary back to a JSON string
masked_json_payload = json.dumps(data, indent=2)
print(masked_json_payload)
Explanation:
Regex Patterns:
- Email: Matches standard email formats.
- Phone: Matches a 10-digit phone number.
- Name: A simple regex to match "FirstName LastName". This can be adjusted based on name formats you expect.
Function mask_data:
- Recursively checks each value in the JSON structure.
- Applies masking based on the regex patterns.
Execution:
- Converts the JSON payload to a dictionary.
- Applies the mask_data function.
- Converts the dictionary back to a JSON string and prints it.
Customization
- Name Regex: The name_regex pattern can be customized to better match your specific name formats.
- Masking Strings: Adjust the replacement strings ("***** ", "@*****.com", "**********") as needed.
Relevant content
- asked 10 months ago
- Accepted Answerasked 2 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 2 years ago