简体   繁体   中英

How do I reference an XML attribute which begins with an @ symbol using KQL extractjson function?

I'm trying to access an XML elements attribute in Azure KQL having converted it to JSON using parse_xml. However the extractjson function doesn't seem to like the use of the @ notation. See the code snippet below.

let input_xml="<NetAmount currency=\"USD\">150.00</NetAmount>";
let sJson=tostring(parse_xml(input_xml));
let amount=extractjson("$.NetAmount.#text", sJson);
let sCurrency=extractjson($.NetAmount.@currency, sJson);
print input_xml, amount,  sJson //, sCurrency;

If you run the above code it will work. However if you comment in the reference to sCurrency in the print statement, it barfs with an error which reads:

There was a problem running your query. Please try again later

Any ideas how you reference the currency attribute in the extractjson function?

you don't need to use extract_json() , rather you can simply use dynamic object accessors: https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/scalar-data-types/dynamic#dynamic-object-accessors

print input_xml = "<NetAmount currency=\"USD\">150.00</NetAmount>"
| project sJson = parse_xml(input_xml)
| project amount = sJson.NetAmount['#text'], currency = sJson.NetAmount['@currency']
amount currency
150.00 USD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM