_Alfred

"In this the love of God was made manifest among us, that God sent his only Son into the world, so that we might live through him." - 1 John 4:9,10.

A simplified json data obtained from www.reddit.com/r/technology/.json to import to MongoDB

By goalfred - Published: 2016-11-05

1. Save www.reddit.com/r/technology/.json (or other reddit.com url you choose) as reddit.json
2. Run this script: node reddit.js
3. Import to MongoDB

/*
   Import onto MongoDB a simplified version of json data obtained from www.reddit.com/r/technology/.json

   input: reddit.json (saved from www.reddit.com/r/technology/.json)
   output: data.json

   usage:
   node reddit.js

   mongodb import:
   mongoimport -d reddit -c tech --drop --type json data.json --jsonArray
*/


var fs = require('fs');
var parsedJSON = '';
var output = []; 

fs.readFile('reddit.json', function(err, data){
  if(err) throw err;
  parsedJSON = JSON.parse(data);

  for(var item in parsedJSON.data.children) {
	output.push ( {
		"id" : parsedJSON.data.children[item].data.id,
		"likes" : parsedJSON.data.children[item].data.likes,
		"author" : parsedJSON.data.children[item].data.author,
		"score" : parsedJSON.data.children[item].data.score,
		"downs" : parsedJSON.data.children[item].data.downs,
		"ups" : parsedJSON.data.children[item].data.ups,
		"permalink" : parsedJSON.data.children[item].data.permalink,
		"url" : parsedJSON.data.children[item].data.url,
		"title" : parsedJSON.data.children[item].data.title,
		"num_comments" : parsedJSON.data.children[item].data.num_comments,
		"created_utc" : parsedJSON.data.children[item].data.created_utc
	} );
  }

  fs.writeFile('data.json', JSON.stringify(output), 'utf8', function(err){
  	console.log('data.json file ready, use mongoimport with --jsonArray to import onto MongoDB');
  });
});