The syntax you are using allows you to navigate the nodes of a JSON object. But it seems you are trying to search treating it like a XPath predicate. It won't work.
With xml2js can obtain an array of time
objects in your code using:
result.root.time
And then, loop comparing the $.c
value, which is how you reach the attributes. For example, the attribute of the second time element is:
result.root.time[1].$.c
So that's the data you need to compare your field. When you discover in which element of the time
array it's in, you get the title
(actually a one-element array containing the title) like this:
var resultArr = [];
for(var i = 0; i < result.root.time.length; i++) {
if (result.root.time[i].$.c == timetag) {
resultArr = result.root.time[i].title;
break;
}
}
Which you can then send to your socket:
io.sockets.emit('subs', { subs: resultArr[0] });
Solution using JSONPath
If you don't want to implement a loop in JavaScript to compare the attribute values, you can use JSONPath. It's syntax is not as nice as XPath, but it works. You will have to get it via NPM and require:
var jsonpath = require('JSONPath');
And then you can obtain the title
you want with this expression:
var expression = "$.root.time[?(@.$.c == '00:00:01')].title[0]";
[..]
is a predicate, ?
is to run a script with an expression, @.
is to access the attribute (which is represented in the xml2js object as $.c
. You can of course replace the string with your timetag
variable.
To run the search, use:
var resultArr = jsonpath.eval(result, expression);
Which will return an array containing the text you want. Then you can send it wherever you want:
io.sockets.emit('subs', { subs: resultArr[0] });
You can read more about JSONPath here: http://goessner.net/articles/JsonPath/