1

I am using CasperJS in order to crawl sites. I have found an interesting thing and I am not really sure why is it happening.

So, with CasperJS (and PhantomJS) you can get into the Page context, using their Evaluate PhantomJS Evaluate CasperJS method.

I have created an object in the page context's, and in order to return it to the CasperJS I convert it to a string. Here is what I do:

casper.then(function() {
    var test = this.evaluate(function() {
        var test_obj = { test_obj: ['test'] }
        console.log("TEST OBJECT VALUE:");
        var JSON_obj = JSON.stringify(test_obj);
        console.log(JSON_obj);
        return JSON_obj;
    });
});

So, in two different pages, the result is different:

When in this page context, the JSON_obj value is:

{"test_obj":"[\"test\"]"}

When in this other page context, the JSON_obj value is:

{"test_obj":["test"]}

This is bad because I catch the results in Ruby, and when doing JSON.parse, for the first case it treats the array as a string... Any idea why JSON.stringify would behave "different" when in different page contexts? Interesting to me!

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Nobita
  • 23,519
  • 11
  • 58
  • 87

1 Answers1

0

I think it might be related to this problem - JSON.stringify() array bizarreness with Prototype.js. When a site has Prototype.js framework installed then JSON.stringify may result in different string than you'd typically expect.

Bajena
  • 181
  • 2
  • 9