How to extract the value of a field found in a string received from a 3rd party server

How to extract the value of a field found in a string received from a 3rd party server ?
In the server response example below, I need to get the QueueID value (in this case 646253102).
Note: the value is not at the same position from the start, from one response to the other because some previous fields have variable length.
Note 2: it looks like XML but not exactly because I modified it because true XML does not print well in this forum.

<(NotifyReturn> <(ResponseCode>0<(//ResponseCode> <(CallAnswered>false<(//CallAnswered> <(QueueID>646253102<(//QueueID> <(TryCount>0<(//TryCount> <(Duration>13<(//Duration> <(CallComplete>false<(//CallComplete> <(//NotifyReturn>

That 3rd party server gives you an XML output. Make sure they don’t also offer a JSON output. Many API’s are selectable, so you may be able to ask for JSON instead. Look into that and let us know.

Squirrel language provides a range of useful string functions.

Probably a combination of string.slice and the string.find methods should do the trick to pick up your QueueID’s and extract out the value. If you needed to pull out other data fields as well there is also the handy string split method to create an array of substrings.

@gerriko, I thought I tried that but I will try again.

@mlseim my 3rd party server mentions nothing about “JSON”, just “SOAP” and “REST”, but I tried it anyways:
if my header is Content-Type=application/x-www-form-urlencoded, it works OK (XML returned), but if Content-Type=application/json, I get an error message.

@gerriko your solution does not work. string.slice is like “pasting” a word in a sentence, string.find returns the position of the QueueID field. string.splice is like “cutting” a word from a sentence. All these functions do not allow me to extract the value of the field QueueID (646253102 in this case).

`function getQueueId(xmlString) {
  local re = regexp2(@"<QueueID>([0-9]+)</QueueID>");
  local results = re.capture(xmlString);
  local queueId = "";
  if(results && results.len() > 1) {
    queueId = xmlString.slice(results[1].begin, results[1].end);
  }
  return queueId;
}`

regexp2 is documented here.

@philmy WOW ! it works! If I become rich I will send you a check for sure ! Thanks you made my day !

@philmy learnt something new too. Cool!

For what it’s worth this is using the string functions (Agent code). Written out the long way:
`
const XML = “<(NotifyReturn> <(ResponseCode>0<(//ResponseCode> <(CallAnswered>false<(//CallAnswered> <(QueueID>646253102<(//QueueID> <(TryCount>0<(//TryCount> <(Duration>13<(//Duration> <(CallComplete>false<(//CallComplete> <(//NotifyReturn>”;

const EXPR = “QueueID”;

function extractQueueID(myxml)
{
local myQID = “”;
local strStart = myxml.find(EXPR) + EXPR.len()+1;
local strEnd = myxml.find("<(//"+EXPR, strStart);
if (strStart && strEnd > strStart)
myQID = myxml.slice(strStart, strEnd);
server.log(myQID);
return(myQID);
}

extractQueueID(XML);
`

@gerriko I can’t make your stuff work :frowning:

@gerriko it gives me empty string…the winner is philmy !

I’ve updated my function above to use regexp2, which is a much better choice for agents.