Search code examples
javascriptjsonphantomjscasperjs

Merge 2 JSON dumps into one (CasperJS)


I'm using CasperJS and I'd like to get one JSON as result.

This is part of my JS :

casper.then(function () {
    require('utils').dump(this.getElementsInfo('h1'));
    this.waitForSelector('video', function () {
        require('utils').dump(this.getElementsInfo('.qual'));
    });
    this.screenshot('ololo');
});

I'm getting two JSON with the "dump" but I'd like to get only one JSON with everything inside.


Solution

  • Since casper.getElementsInfo produces objects that have always the same properties, it wouldn't make sense to integrate one object into another, because the properties would be overwritten. But you can make another object that contains the sub-objects:

    casper.then(function () {
        var h1 = this.getElementsInfo('h1');
        this.waitForSelector('video', function () {
            var qual = this.getElementsInfo('.qual');
            require('utils').dump({
                h1: h1,
                qual: qual
            });
        });
        this.screenshot('ololo');
    });