javascriptnode.jssocket.iorequestjs

Find JSON is Changed in JSON Object


I'm working request data from a URL for every seconds

exports.getFootballNotRunning = function (callback) {

request({
    method: 'GET',
    url: 'http://ufxyz.ufabet.com/_View/RMOdds1Gen.ashx?ot=t&sort=0&at=EU',
}, function (error, response, body) {
    body = body.replace(/'/g, '"');
    body = JSON.parse(body);
    var setoffootball = [];
    for (var i = 0; i < body[2].length; i++) {
        setoffootball[i] = {
            league: body[2][i][0][1],
            matches: []
        };
        for (var j = 0; j < body[2][i][1].length; j++) {
            setoffootball[i].matches.push({
                firstteam: body[2][i][1][j][21],
                secondteam: body[2][i][1][j][23],
                time: body[2][i][1][j][10],
                fulltime: {
                    hdp: body[2][i][1][j][19],
                    h: body[2][i][1][j][32],
                    a: body[2][i][1][j][33],
                    goal: body[2][i][1][j][38],
                    over: body[2][i][1][j][39],
                    under: body[2][i][1][j][40]
                },
                firsthalf: {
                    hdp: body[2][i][1][j][49],
                    h: body[2][i][1][j][53],
                    a: body[2][i][1][j][54],
                    goal: body[2][i][1][j][57],
                    over: body[2][i][1][j][58],
                    under: body[2][i][1][j][59]
                }
            });
        }

    }
    notrunningfootball = setoffootball;
    callback(setoffootball);

});

}

just like this and every seconds i want to check that the Array JSON that I recieved which json is different from the previous one then I can send the JSON that changed through socket.io and update in the table of client-side

var io = require('socket.io')();
io.on('connection', function (socket) {
    console.log('test');
    sendMatches();
});
io.listen(7000);

function sendMatches() {
    setTimeout(function () {
        FT.getAllMatches(function (run, notrun) {
            io.emit('running', run);
            io.emit('notrunning', notrun);
            sendMatches();
        })
    }, 1000);
}

JSON Example , it's array that have many json object in there the example is just one json

[{"league":"ENGLISH PREMIER LEAGUE","matches":[{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0.25,"h":-9.7,"a":9.3,"goal":2,"over":8.6,"under":-9.3},"firsthalf":{"hdp":0,"h":6.9,"a":-7.8,"goal":0.75,"over":8.5,"under":-9.4}},{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0,"h":6.7,"a":-7.1,"goal":2.25,"over":-8.4,"under":7.7},"firsthalf":{"hdp":0.25,"h":-6.8,"a":5.9,"goal":1,"over":-7.3,"under":6.4}},{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0.5,"h":-7.2,"a":6.8,"goal":1.75,"over":6.5,"under":-7.2},"firsthalf":{"hdp":0,"h":0,"a":0,"goal":0,"over":0,"under":0}}]}]

The flow is like this Run app.js -> Server side: request data -> Server side: send whole data to client -> Client : fetch all data into table -> next 1 seconds -> Server side: request data - > Server side : compare the old data and new data and find the json that change - > Server side: send the json that different from the previous one to client -> Client : Update the new json to table


Solution

  • I don't think I got you right.. If you just want to check if there is a difference, you could simply compare stings?..

    JSON.stringify(a) === JSON.stringify(b)

    That would tell you if there is a change at all. To find the difference you can do strings manipulations or use underscore Lib (_) to help you out.

    var o1 = {a: 1, b: 2, c: 2},
        o2 = {a: 2, b: 1, c: 2};
    
    _.omit(o1, function(v,k) { return o2[k] === v; })
    

    Results in the parts of o1 that correspond but with different values in o2:

    {a: 1, b: 2}
    

    It'd be different for a deep diff:

    function diff(a,b) {
        var r = {};
        _.each(a, function(v,k) {
            if(b[k] === v) return;
            r[k] = _.isObject(v)
                    ? _.diff(v, b[k])
                    : v
                ;
            });
        return r;
    }