0

I wrote code that parsing a lot of words (innerHTML) from some webpages.

and I'd like to insert data to json file directly..

Here is my js code...

var words = [];
var casper = require('casper').create();

    function getWords() {
        var words = document.querySelectorAll('td.subject a');
        return Array.prototype.map.call(words, function(e) {
            return e.innerHTML;
        });
    }


        casper.start('http://www.todayhumor.co.kr/board/list.php?table=bestofbest', function() {                
            words = this.evaluate(getWords);
        });

        for (var i=2; i <=5; i++) {
        casper.thenOpen('http://www.todayhumor.co.kr/board/list.php?table=bestofbest&page='+i, function() {              

            words = words.concat(this.evaluate(getWords));
        });
        }

    casper.run(function() {
        // echo results in some pretty fashion
        this.echo(words.length + ' links found:').exit();
        this.echo(words.join('\n')).exit();
});

and

I run this code through terminal like this!

username@wow:~/workspace/app/assets/javascripts $ casperjs application.js

and the result is (for example)

150 words found:
apple
banana
melon
kiwi
citrus
watermelon
passionfruit
mango
orange
...

So I want to insert this data in "word" part of my json file (example code of json below)

and make other columns("type": "fruit" and "spell":) automatically added

{ "my_initial_words": [
    {
    "type": "fruit",
    "word": "apple",
    "spell": "ap"
    },
    {
    "type": "fruit",
    "word": "banana",
    "spell": "ba"
    },
    {
    "type": "fruit",
    "word": "melon",
    "spell": "me"
    }   

]
}
----------------------------------------------------------------------------

thanks for adding more answer!.. but I couldn't catch where should I put these code

Could you tell me once more that... Which code you gave me executes "Saving the results to JSON file?" because I have to read json file(makeyourap.json) in my seeds.rb file like this

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
Hyung Kyu Park
  • 255
  • 2
  • 3
  • 12
  • What's the rule to generate the `type` and `spell` properties? – Artjom B. Sep 02 '15 at 18:31
  • Actually i'll parse data like this.. 1. search "fruit" and 2. get innerhtml results of webpages. So in case of "type", I think I have to write "fruit" directly then do iteration and "spell" is "word"s' first and second spelling! – Hyung Kyu Park Sep 02 '15 at 19:15

2 Answers2

2

So, something like this?

function makeTypeObject(name, type) {
  return {
    name: name,
    type: type,
    spell: name.substr(0,2)
  };
}

var wordDesc = words.map(function (word) { 
   return makeTypeObject(word, "fruit"); 
});

var finalObject = {
  my_initial_words: wordDesc
};

var jsonString = JSON.stringify(finalObject);
// if you want prettyprint, try JSON.stringify(finalObject, null, "\t");

I hope this helps.

Mutahhir
  • 3,812
  • 3
  • 21
  • 26
1

Write to file via casper

If you want to have a file from which you read and write, appending content, you can do it like this:

var fs = require('fs');
var FILENAME = 'makeyourap.json';
function add_new_fruits(fruits) {
    var data;
    if ( fs.isFile(FILENAME) ) {
        data = fs.read(FILENAME);
    } else {
        data = JSON.stringify({'my_initial_words' : [] });
    }
    var json = JSON.parse(data);
    fruits.forEach(function(word) {
        json.my_initial_words.push({"type": "fruit",
                                    "name": word,
                                    "spell": word.slice(0,2)});
    });
    data = JSON.stringify(json, null, '\t');
    fs.write(FILENAME, data, "w");
}

Use this instead of the older this.echo. Just call it as

casperjs application.js

This either reads the object from a file, or creates it if it does not exist. Then, it appends each new object from the new fruits (including duplicates), and writes it back to FILENAME.

Previous approach: how to roll your own

create Object

So first, you want to create an object that only has the parameter my_initial_words with values as above.

You can create a function via

function createFinal(wordArray) {
    var out = [];
    wordArray.forEach(function(word) {
        out.push({"type": "fruit", "name": word, "spell": word.slice(0,2)});
    });
    return out;
}

to create the array. Then, create the object via

var my_object = { "my_initial_words": createFinal(words) };

to JSON

Javascript has a built-in JSON-object. With a javascript-object like

var my_object = { "my_initial_words": ...

as above, use

JSON.stringify(my_object) 

to get the JSON representation to write.

Older: write to file via redirection

Before, you had

this.echo(words.join('\n')).exit();

which gave you the basic list. Using this.echo, try replacing this by

var my_object = { "my_initial_words": createFinal(words) };
this.echo(JSON.stringify(my_object)).exit();

This prints to standard output. Just remove the other this.echo line (150 words found) and redirect the output via

casperjs application.js > makeyourap.json

If you want to write to file in casperjs, look at write-results-into-a-file-using-casperjs.

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • Sorry i'm very new to js and programming.. It is little hard for me to understand...you mean I don't have to add any function() code in js file?(in casperjs code) – Hyung Kyu Park Sep 02 '15 at 19:25
  • @HyungKyuPark: sure. I thought it was only about the JSON. Function creating the object is appended (the other answer looks nice, too) – serv-inc Sep 03 '15 at 12:02
  • 1
    Thank you for your more detail answer! but I couldn't catch the point of it because I'm not good at english or sth..... So Could you please read last part of my question once more and answer me? I added several line of it! – Hyung Kyu Park Sep 03 '15 at 12:35
  • Or does it mean that I have to require js file in json file?? – Hyung Kyu Park Sep 03 '15 at 13:22
  • @HyungKyuPark: Have a look at the snippet to write to file. – serv-inc Sep 03 '15 at 14:03
  • Oh it works well! I really appreciate it... but when I execute casperjs application.js > makeyourap.json this command rewrite all the things.. how can I add data..?? – Hyung Kyu Park Sep 03 '15 at 15:43
  • @HyungKyuPark: See the edit. Next time, post what you tried and what did (or did not) work. – serv-inc Sep 03 '15 at 16:58
  • 1
    I see thank you so much user------- and I realized that when I have to append contents fs.write('.json', result, 'a'); ! – Hyung Kyu Park Sep 04 '15 at 07:40
  • @HyungKyuPark: You are very welcome. Yet, appending a JSON document to a JSON document does not yield a correct JSON document. That's why SO recommends to first parse the original document and then write a modified document back (with `w`), see http://stackoverflow.com/questions/12290572/appending-to-json-file-in-javascript and http://stackoverflow.com/questions/17518801/how-to-append-json-to-already-existing-file-with-javascript – serv-inc Sep 04 '15 at 08:07