Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CasperJs loads json data from a local file

Is there any convenient way to load a local JSON file into a variable with CasperJs?

I saw someone suggest to use

$.getJSON(filename, function() ... 
like image 989
marsant Avatar asked Aug 26 '13 02:08

marsant


3 Answers

I have the following working on CasperJS 1.1-beta1 and PhantomJS 1.9.1

test.json

{
    "test": "hello"
}

test.js

var json = require('test.json');
require('utils').dump(json);
casper.echo(json.test); // "hello"
like image 78
hexid Avatar answered Nov 07 '22 19:11

hexid


The solution proposed by @hexid worked for me with one change, i added a './' before the file address to denote it is a local file.

test.json

{
    "test": "hello"
}

test.js

var utils = require('utils');
var json = require('./test.json');

utils.dump(json);
utils.dump(json.test); // hello
utils.dump(json["test"]); // hello

(i would add it as a comment but I'd need 50+ rep to do that)

like image 6
VinGarcia Avatar answered Nov 07 '22 21:11

VinGarcia


Here is a complete sample

var casper = require('casper').create();

var json = require('test.json');
require('utils').dump(json);
casper.echo(json['test']);

casper.exit();
like image 3
marsant Avatar answered Nov 07 '22 21:11

marsant