Extract text from multiple tweets in JSON format - javascript

I have extracted 1000 tweets that mention my internship company. So, I received the data in JSON format. Now, I am trying to extract the text for each 1000 tweets of the JSON
Since the JSON is stored as notepad, so I used require('fs') and also readFileSync because of synchronous actions
const fs=require('fs')
var lineTNB = fs.readFileSync('mentionTNB.json','utf8',function(err,data) {
if (err) throw err;
obj = JSON.parse(data);
});
var i;
for (i=0;i<lineTNB.length;i++){
console.log(lineTNB[i].text);
}
The expected values would be
"text": "string1234"
"text": "string7682"
The actual values:
undefined
undefined

Assuming you still want it to be sync:
var res = JSON.parse(fs.readFileSync('mentionTNB.json', 'utf8'));
console.log(res)
If you actually meant you want it async with callback:
var res = fs.readFile('mentionTNB.json', 'utf8', function (err, data) {
if (err) throw err;
return JSON.parse(data);
});

Related

How can I convert this NewsApi return into a JSON FIle?

I am just beginning to dabble into this API and JavaScript in general. I took a coding class at school last year and learned what I believe to be the basics. The API I am using returns a list of news articles and their info as a JSON Array in the console. The current code I have is:
const fs = require('fs');
let apiKey = "myKey"
console.log(apiKey);
const NewsAPI = require('newsapi');
const { json } = require('stream/consumers');
const newsapi = new NewsAPI(apiKey);
var globalResponse
newsapi.v2.everything({
q: 'trump',
}).then(response => {
console.log(response);
globalResponse = response;
})
// parse json
var jsonObj = JSON.parse(globalResponse);
// stringify JSON Object
var jsonContent = JSON.stringify(jsonObj);
console.log(jsonContent);
fs.writeFile("output.json", jsonContent, 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("JSON file has been saved.");
});
I can't seem to get the returned JSON to become a file. Its always one error after another

Parsing data in javascript with selenium

I am trying to read data from a json file in javascript. I can read succesfully read from it. When passing in the data in the code it throws errors.
Any help will be appriciated.
My code is as follows :
module.exports = {
'#tags': ['TC2'],
"LAUNCHURL" : function (browser) {
var fs = require('fs');
fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
if (err) throw err; // we'll not consider error handling for now
var mydata = JSON.parse(data);
var url_get = mydata.credentials[0]['url'] //Passing data works here
browser
//opens salesforce and checks title to match 'Login | Salesforce'
.url(url_get)
.waitForElementVisible('//body', 1000)
//.assert.title('Login | Salesforce')
});
},
"Login": function(browser) {
var fs = require('fs');
fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
if (err) throw err; // we'll not consider error handling for now
var mydata = JSON.parse(data);
var email = mydata.credentials[0]['email']
browser
.useXpath()
.click("//a[contains(text(),'Sign In')]")
.waitForElementVisible('//body',1000)
.setValue("//input[#aria-label='Enter email address']", email )//Passing data does NOT work here
.click("//button[#type='button']")
.waitForElementVisible("//input[#id='password']")
.setValue("//input[#id='password']","password12345")
.click("//button[#type='button']")
});
}
};
My json file is as follows
{ "credentials": [ {"url": "https://www.walmart.com", "search": "bandaids", "email" : "test#yahoo.com", "password" : "password12345"}] }
Further info
When the values are hard coded, the script works. As soon as a attempt to pass in data in breaks the code and wont complete. Using nightwatch as a test tool.
The paths are different which might be the problem:
LAUNCHURL: fs.readFile('C:/NightWatch_Automation/credentials.json'
Login: fs.readFile('C:/Users/NightWatch_Automation/credentials.json'

Json Decode and Parse Error node.js [duplicate]

This question already has answers here:
Loading and parsing a JSON file with multiple JSON objects
(5 answers)
Closed 5 years ago.
Hi I'm trying to do this
const request = require('request');
const zlib = require('zlib');
const opts = {
uri: 'http://data.githubarchive.org/2015-01-01-15.json.gz',
encoding: null,
};
request.get(opts, function(error, response, body) {
if (!error) {
zlib.gunzip(body, function(err, decoded) {
if (err) {
console.log(err)
} else {
var json_string = decoded.toString('utf-8').replace(/(\r\n|\n|\r)/gm, " ").trim();
var json = JSON.parse(json_string);
console.log("SJON", typeof json)
}
});
}
});
I'm following the below steps:
fetching data from url
unzip that using zlib.gunzip
converting that decoded data to string
replacing all newline and beak statements
I'm trying to parse that string which throws error
I'm getting error while parsing data using JSON.parse, this is public dataset of github. I don't know where I'm going wrong, can any one help.
That file contains one JSON object per line. Just removing the newlines won't yield a valid single JSON object. Instead, you can split the input data on newlines, and parse each line separately:
zlib.gunzip(body, function(err, decoded) {
if (err) {
console.log(err);
} else {
let array = decoded.toString().trim().split(/\r?\n/).map(line => JSON.parse(line));
...
}
});

NodeJs: can't write a file

I'm new in node, for practice i thought to develop a weather commandline application, but i found a problem with ajax request, i'm usually to use $.ajax of jquery but it doesn't works, ( I've tried to require jquery ). I've solved this problem with another module.
Now the problem is: when i try to print json information on the coords.json and next read it with read-json module there are some "\" & "\n" everywhere in the string, i've tried to replace it with regex and fs module but it doesn't re-write the file... why?
Here the full code:
// index.js
// modules
const program = require('commander');
const clear = require('clear');
const chalk = require('chalk');
const request = require('ajax-request');
const fs = require('fs');
const json = require('read-data').json;
const writeJson = require('write-json');
// Forecast.io Key
const key = "*************";
const freegeoip = "http://freegeoip.net/json/";
let latitude = 0,
longitude = 0 ;
// forecast.io api url
const url = `https://api.darksky.net/forecast/${key}/${latitude},${longitude}`;
// initialize myData with the freegeoip datas
let myData = request({
url: 'http://freegeoip.net/json/',
method: 'GET',
data: {
format: 'json'
},
}, function(err, res, body) {
writeJson('test.json', body, function(err) {
if (err) console.log(err);
});
});
fs.readFile('test.json', 'utf8', function (err,data) {
let result = data.replace(/[\\~#%&*<>?|\-]/g, '');
fs.writeFile('test.json', result, 'utf8', function (err) {
if (err) return console.log(err);
// if i do this is normal json
// console.log(result)
});
});
and the output in the file is:
// coords.json
"{\"ip\":\"**.**.**.**\",\"country_code\":\"IT\",\"country_name\":\"Italy\",\"region_code\":\"62\",\"region_name\":\"Latium\",\"city\":\"Rome\",\"zip_code\":\"00119\",\"time_zone\":\"Europe/Rome\",\"latitude\":**.*,\"longitude\":**.**\"metro_code\":0}\n"
but if i print it in console it's normal...
I really recommend that you use JSON.parse. It will parse your json and put it into a variable you can use:
fs.readFile('test.json', 'utf8', function (err,data) {
data = JSON.parse(data); // Yay you can use anything from the JSON
}
The \ are there to escape the quotes so that they don't end the string. They shouldn't affect anything, and are actually necessary. Have you tried it without the regex? That could be breaking things if it actually removes the .

Writing MongoDB result to file using native Node.js driver

I am trying to write the results of a MongoDB query to a file using the native Node.js driver. My code is the following (based on this post: Writing files in Node.js):
var query = require('./queries.js');
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
if(err) { return console.dir(err); }
var buildsColl = db.collection('blah');
collection.aggregate(query.test, function(err, result) {
var JSONResult = JSON.stringify(result);
//console.log(JSONResult);
fs.writeFile("test.json", JSONResult, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});
collection.aggregate(query.next, function(err, result) {
var JSONResult = JSON.stringify(result);
//console.log(JSONResult);
db.close();
});
});
The file is written, but the contents are 'undefined.' Printing the result to the console works though.
Your code is not checking the err on the aggregate callback.
You are likely getting an Mongo error and the result is undefined in that case...
Other thing I could suspect is that you are getting multiple callbacks -- each one of them creates a new files, erasing the content.
Try using fs.appendFile instead of fs.writeFile and see if you are getting the expected data (plus the unwanted undefined)
For anyone stumbling across this the solution on where to put the db.close() is below:
collection.aggregate(query.test, function(err, result) {
var JSONResult = JSON.stringify(result);
//console.log(JSONResult);
fs.writeFile("test.json", JSONResult, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
collection.aggregate(query.next, function(err, result) {
var JSONResult = JSON.stringify(result);
//console.log(JSONResult);
db.close();
});
});

Categories

Resources