Skip to main content

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 value "AS15169" and "as" with value "Google LLC" in two different keys from "org" key. 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 ASN and AS
asn_part, as_part = org_value.split(" ", 1)

# Prepare the response
response = {
    "asn": asn_part,    # ASN value (e.g., AS15169)
    "as": as_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"
}