Json Decode and Parse Error node.js [duplicate] - javascript

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));
...
}
});

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

Extract text from multiple tweets in JSON format

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);
});

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'

Node.js - Get a single XML value

I am simply trying to output a single value (thumbnail) of an XML file in Node.js. I feel like I am so close but can't figure it out.
var request = require('request');
request('https://boardgamegeek.com/xmlapi/game/1', (error, response, body) => {
if (error) { return console.log(error); }
console.log(body.thumbnail);
});
You need a XML parser, for example xml2js :
var request = require('request');
var parseString = require('xml2js').parseString;
request('https://boardgamegeek.com/xmlapi/game/1', (error, response, body) => {
if (error) { return console.log(error); }
parseString(body, function (err, result) {
console.dir(result);
});
});
Double check by using the console to see all of body I.e:
console.log(body)
Then you will see the options you have available. Show us what you get and we could be more specific or it may be enough for you to work out at a glance. You are on the right track. It just depends on the data structure that is there for you.

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 .

Categories

Resources