0

I'm trying to parse data by dates in a JSON and having problems.

Essentially, I want to grab events that are between two dates/times, but am having virtually no luck doing this. I know there are JSON parsers out there that I could use, but most that I've found will extract a field.

I don't want to extract a field - I want the raw JSON for all events that are after a certain date.

So in my data below, I want to grab all events (all 4 rows of each JSON entry) that are after 2017-01-14 2:00am. How can I accomplish this?

{
    "_id": "58816d03e4b00654468d2781",
    "datetime": "2017-01-03T05:23:02Z",
    "msg": "foo1"
    "msg2": "foo2"
},
{
    "_id": "58816d03e4b00654468d2963",
    "datetime": "2017-01-14T01:50:52Z",
    "msg": "foo1"
    "msg2": "foo2"
},
{
    "_id": "58816d03e4b00654468d3068",
    "datetime": "2017-01-16T13:41:46Z",
    "msg": "foo1"
    "msg2": "foo2"
},
{
    "_id": "58816d03e4b00654468d3068",
    "datetime": "2017-01-20T21:16:40Z",
    "msg": "foo1"
    "msg2": "foo2"
},
bdf0506
  • 35
  • 6

1 Answers1

0

If your 'event' objects are in an array you could use the array.filter() method to return all objects that pass a test (in your case it would be if the datetime prop was after a certain time)

var data,
    filteredData,
    testDate = new Date("2017-01-14T02:00:00Z");

data = [
    {
        "_id": "58816d03e4b00654468d2781",
        "datetime": "2017-01-03T05:23:02Z",
        "msg": "foo1",
        "msg2": "foo2"
    },
    {
        "_id": "58816d03e4b00654468d2963",
        "datetime": "2017-01-14T01:50:52Z",
        "msg": "foo1",
        "msg2": "foo2"
    },
    {
        "_id": "58816d03e4b00654468d3068",
        "datetime": "2017-01-16T13:41:46Z",
        "msg": "foo1",
        "msg2": "foo2"
    },
    {
        "_id": "58816d03e4b00654468d3068",
        "datetime": "2017-01-20T21:16:40Z",
        "msg": "foo1",
        "msg2": "foo2"
    }
]

filteredData = data.filter(function(d){
    return new Date(d.datetime) > testDate;
});

With example http get request for data

var filteredData,
    testDate = new Date("2017-01-14T02:00:00Z");

function httpGetAsync(theUrl, callback){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

function filterResponseByDate(data){
     filteredData = data.filter(function(d){
        return new Date(d.datetime) > testDate;
    });
}

httpGetAsync(yourDataURL,filterResponseByDate);
haydenwagner
  • 546
  • 2
  • 6
  • 15
  • Thanks, that does seem to work. What if my data is somewhere else, such as in a flat file? How can I embed the data within this code? Also, how can I change the after date to be a relative field, such as one day ago? – bdf0506 Jan 20 '17 at 19:58
  • If your data is somewhere else you will need to make a request to a server. If you did this, then you would have the http request along with this code, the response from the http request would be your data, and once you received it you would filter the data. Date objects and time in general in javascript can be pretty tricky, but you should be able to subtract a day from a new date object pretty easily. Check this: http://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript – haydenwagner Jan 20 '17 at 20:27
  • So maybe I'm misunderstanding this. Let's assume that the data about is at the page "http://foo/bar/data". If i grab that page with a simple wget, that page would get me a JSON of everything that I need. How do you integrate that in, where I could extract simply all events that happened after a relative date of now - 1 hour? – bdf0506 Jan 20 '17 at 21:01
  • If I understand you correctly you want to make the get request from within the javascript----once you have a response to that request then you can manipulate that data however you want (filter some events). – haydenwagner Jan 20 '17 at 21:14
  • I made an example to show an http request...its not organized in the best way but it should give you an idea of how you could make a request and then filter the response. – haydenwagner Jan 20 '17 at 21:23
  • Yes. assume that `http://foo/bar/data` actually is the resource that I grab the data from. How do i write it so that the js will grab the data from that above http site, then parse as shown in the first example you gave? – bdf0506 Jan 20 '17 at 21:27
  • check the edit on my answer, the second code block gives you a starting point...the function call at the bottom would look like this: httpGetAsync('http : // foo/bar/data', filterResponseByData); – haydenwagner Jan 20 '17 at 21:39
  • Looks like this doesn't quite work on node 6.9.1. `> httpGetAsync('http://google.com',filterResponseByDate); ReferenceError: XMLHttpRequest is not defined at httpGetAsync (repl:3:19) at repl:1:1 at sigintHandlersWrap (vm.js:22:35) at sigintHandlersWrap (vm.js:96:12) at ContextifyScript.Script.runInThisContext (vm.js:21:12) at REPLServer.defaultEval (repl.js:313:29) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer. (repl.js:513:10) at emitOne (events.js:101:20) > ` – bdf0506 Jan 21 '17 at 00:55
  • yes, I don't know anything about your program and you haven't mentioned node before this. This is a simple http request that will run in a browser on the front end, not on a node server. There is plenty of documentation on http requests in node – haydenwagner Jan 21 '17 at 03:09