Split String with Regular Expression
Incoming data
{
"ip": "xx.249.74.xxx",
"hostname": "crawl-xx.249.74.xxx.googlebot.com",
"city": "Tulsa",
"region": "Oklahoma",
"country": "US",
"loc": "36.1540,-95.9928",
"org": "AS15169 Google LLC",
"postal": "74102",
"timezone": "America/Chicago"
}
In the above dataset we need the "asn" with "AS15169" and "asn" with "Google LLC" in two different keys. Below is the pycode to enrich this
#sending to input of operation
Responsedata = []
# Extract the value of the "org" key
org_value = pycode_data.get("org", "") # Default to an empty string if "org" is missing
# Split the "org" value into AS and ASN
asn_part,
as_partas_part, asn_part = org_value.split(" ", 1)
# Prepare the response
response = {
"asn": asn_part,as_part, # ASN value (e.g., AS15169)
"as": as_partasn_part # AS (e.g., Google LLC)
}
#sending to output of operation
pycode_data.update(response)
Below is the output of the operation, it has appended with two more keys as "asn" and "as"
{
"ip": "xx.249.74.xxx",
"hostname": "crawl-xx.249.74.xxx.googlebot.com",
"city": "Tulsa",
"region": "Oklahoma",
"country": "US",
"loc": "36.1540,-95.9928",
"org": "AS15169 Google LLC",
"asn": "AS15169",
"as": "Google LLC",
"postal": "74102",
"timezone": "America/Chicago"
}