Search code examples
kqlazure-data-exploreraskql

Extract query string from url


Looking for an approach to extract the query string from the URL such that a scenario where the query string is empty or not present is also addressed. I have following but would be helpful if there is a better approach.

let qryStr = "b="; // 'b' is the query-stirng. Need to extract its value
let T = datatable(url: string)
    [
    "https://example.com/abc.htm?a=1&b=2&c=3",
    "https://example.org/abc.htm?a=1&c=3&b=2",
    "https://example.net/abc.htm?b=2&a=1&c=3",
    "https://example.net/abc.htm?a=1&c=3",
    "https://example.net/abc.htm?a=1&c=3&b="
];
T
| project url
| extend s1 = indexof(url, qryStr)
| where s1 > 0
| extend value=split(substring(url, s1 + strlen(qryStr)), "&")[0]
| project url, ['qryStr'], value, s1

Solution

  • You can try with parse_url().

    let qryStr = "b"; // 'b' is the query-stirng. Need to extract its value
    let T = datatable(url: string)
        [
        "https://example.com/abc.htm?a=1&b=2&c=3",
        "https://example.org/abc.htm?a=1&c=3&b=2",
        "https://example.net/abc.htm?b=2&a=1&c=3",
        "https://example.net/abc.htm?a=1&c=3",
        "https://example.net/abc.htm?a=1&c=3&b="
    ];
    T
    | project url
    | extend value = parse_url(url)["Query Parameters"][qryStr]
    | project url, ['qryStr'], value