JSON Parsing Issue Caused by foreign characters? - javascript

I am trying to parse a field of return JSON data, from an API which has a lot of strange characters in it (East Asian Symbols, curly quotes etc.). I am getting this error and do not know how to fix it. Is there a way to convert the request response to some format that "escapes" the bad text.
Here is the error:
Here is my exact code, I am sorry it is so long.
var fs = require('fs'),
http = require('http'),
request = require('request');
var url = 'http://app.sportstream.com/api/tagstream/tagStreamForStageTwoModeration?q={%22customerId%22:%22ABHoko14%22,%22type%22:{%22$in%22:[%22image%22,%22video%22]}}&_=_5555'
var firstTime = false
var m = 1
function get() {
console.log('ghghg')
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = JSON.parse(body)
jParse(body)
setTimeout(get, 5000)
});
}).on('error', function(e) {
console.log("Got error: ", e);
setTimeout(get, 5000)
});
}
function jParse(info) {
//data = JSON.parse(info)
data = info
entries = data.entries
if (firstTime) {
numEntries = 800 //entries.length
firstTime = false
} else {
numEntries = 2
//numEntries = entries.length - numEntries
if (numEntries) {
for (i = 0; i < numEntries; i++) {
type = entries[i]['type']
title = entries[i]['author']
if (type == 'video') {
url = entries[i]['ssMetaData']['videos']['standard_resolution']['url']
download(url, 'images/aFile.mp4', function() {
console.log('hello')
})
} else if (type == 'image') {
url = entries[i]['ssMetaData']['images']['standard_resolution']['url']
download(url, 'images/' + m + 'File.jpg', function() {
console.log('hello')
})
} else {
console.log('no data')
}
m++
}
}
}
}
get()
And here is my error
undefined:1
����
^
SyntaxError: Unexpected token �
at Object.parse (native)

By foreign characters; I assume you are referring to UTF-8 encoded string.
Why don't you try setting the Content-Type encoding on the response like this:
http.get(url, function(res) {
res.header("Content-Type", "application/json; charset=utf-8");

Related

Why do I get a SyntaxError: Unexpected end of input?

Running a node server and I am getting the following error.
SyntaxError: Unexpected end of input
var http = require('http');
var socketio = require('socket.io');
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
function log_me(msg){
var ts = new Date(new Date().getTime() - (3600000*4));
var tss = ts.toString();
tss = tss.substring(0, tss.indexOf(' GMT'));
console.log(tss + ": " + msg);
}
var app = http.createServer(function(req, res) {
var postData = "";
req.on('data', function(chunk) {
postData += chunk; //Get the POST data
});
req.on('end', function() {
if (typeof(postData) !== "undefined") {
var message = JSON.parse(postData); <-- Here is the issue line 25
//Do something here
//Todo...
}
});
res.end();
}).listen(8080); //Use a non-standard port so it doesn't override your Apache
var io = socketio.listen(app);
//var io = require('socket.io').listen(8080,'0.0.0.0');
io.set('log level', 2);
// io.set('transports', ['flashsocket', 'polling', 'websocket']);
io.set('origins', '*:*');
You can use something like that:
JSON.safeParse = function(data) {
try {
return JSON.parse(data);
} catch (e) {
return false;
}
}
Change your JSON.parse call to JSON.safeParse, and then check if the result is valid:
var message = JSON.safeParse(postData);
if (message) {
// valid!
} else {
// invalid
}

How to store multiple JavaScript objects from a file to a variable

So this how the app should work: with a Node.js script I call the coincap.io api, and I store the response of the various request in different files. Here is the script:
var request = require("request");
var fs = require("fs");
var cors = require("cors");
var parseString = require("xml2js").parseString;
var coinstore = [];
var endpoint = ["coins", "map", "front", "global"];
for (i = 0; i < endpoint.length; i++) {
request("http://coincap.io/" + endpoint[i], function(err, response, body) {
console.log("error", Error);
console.log("statusCode: ", response && response.statusCode);
//console.log("body: ", JSON.stringify(body));
var xml = body;
parseString(xml, function(err, result) {
console.log(xml);
coinstore.push(xml);
});
fs.writeFile("D:/bibblebit/response" + response.request.path.replace(/\//g, "") + ".json", coinstore,
function(err) {
if (err) {
console.log(err);
} else {
console.log("salvato!");
}
});
});
};
I then let the user make custom calls and retrieve data from those files.
Here is a working call:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log("pronto!");
var alldata = [];
alldata.push(xhr.response);
var obj = JSON.parse(alldata);
console.log(obj);
document.getElementById("api-container").innerHTML += obj.altCap;
} else {
console.log("try again");
}
}
xhr.open("GET", "http://127.0.0.1:8081/responseglobal.json", true);
xhr.send(null);
This works because the file responseglobal.json has a single object.
Changing the last snippet into this one:
document.getElementById("api-container").innerHTML += obj.mktcap;
}
else {
console.log("try again");
}
}
xhr.open("GET", "http://127.0.0.1:8081/responsefront.json", true);
xhr.send(null);
returns a self explanatory error:
[Visualizza/nascondi dettagli messaggio.] SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 264 of the JSON data
Every comma separating objects results in an unexpected character, which means I am not able to create one object for every object in the file or at least read a file with more than one object. What am I missing?
I may have missed the right approach. I can't find on the net a fitting explanation, since the questions I was able to find refer to manually created objects or so specific scenarios to become useless in my case to a different extent.
This is the server side script:
var request = require("request");
var fs = require("fs");
var cors = require ("cors");
var parseString = require("xml2js").parseString;
var endpoint = ["coins", "map","front", "global"];
var i = 0;
for (i=0 ; i<endpoint.length ; i++){
request("http://coincap.io/"+endpoint[i], function (err, response, body){
var coinstore = [];
console.log("error", Error);
console.log("statusCode: ", response && response.statusCode);
var xml =JSON.stringify(body);
parseString(xml, function (err, result){
console.log(xml);
coinstore.push(xml);
});
fs.writeFileSync("/response"+ response.request.path.replace(/\//g, "")+ ".json", coinstore, function(err){
if (err){
console.log(err);
}
else {
console.log("salvato!");
}
});
});
};
And this one is the client-side one:
var queries = [ "global", "coins", "map","front"];
for(i=0; i<queries.length; i++) {
pronto(i);
}
function pronto(i) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE) {
console.log("pronto!");
var obj = JSON.parse(xhr.response);
//console.log(obj);
document.getElementById("api"+i+"-container").innerHTML+=obj;
console.log("stampato: "+ i);
} else {
console.log("bucato");
}
}
xhr.open("GET", "response"+ queries[i] +".json", true);
xhr.send(null);
}
They might not be the best solutions, but they work out.

indexOf() doesn't work

I got an array of strings and I'd like to check if any of these strings contain a certain substring.
The following code is an example of what I need to achieve: the function gets a message from an user and searches through a series of URLs the one that contains the user's message in the title:
var redditSubModule = "node";
var http = require('https');
var url = "https://www.reddit.com/r/soccerstreams/new/.json";
var i = 0;
var subject = (ctx.message.text).substring(8);
var request = http.get(url, function(response) {
var json = '';
response.on('data', function(chunk) {
json += chunk;
});
response.on('end', function() {
var redditResponse = JSON.parse(json);
redditResponse.data.children.forEach(function(child) {
if(child.data.domain !== 'self.node') {
url = child.data.url;
console.log('URL : ' + url);
if (url.indexOf(subject) > -1) {
console.log('URL : ' + url);
} else {
console.log("nothing...");
}
i++;
}
});
})
});
request.on('error', function(err) {
console.log(err);
});
If you try and fill subject with "USA" (because there's a thread with that string in the title) the indexOf doesn't seem to work and it prints a list of "nothing..."
Logging the typeof url gives string as type so I don't know what is going on here...
What am I doing wrong ?
You can use array.prototype.find and array.prototype.includes:
var strs = ["aaa", "xxx", "yyy_leon"];
var search = "leon";
var found = strs.find(s => s.includes(search));
if (found) {
console.log("found: ", found);
} else {
console.log("nothing");
}

TypeError: curl.setopt is not a function]

I am a newbie and trying to create an application based on poloniex.js API getting error-TypeError: curl.setopt is not a function] set node-curl(not working) and node-libcurl (partially works,but the function seems incorrectly expressed) slightly confused between the two curl) node-curl is outdated and maybe that's the problem-can you tell what is wrong?
'use strict';
var autobahn = require('autobahn'),
crypto = require('crypto'),
async = require('async'),
https = require('https'),
nonce = require('nonce')(),
querystring = require('querystring'),
Curl = require('node-libcurl').Curl,
microtime = require('microtime'),
events = require('events'),
util = require('util');
var Poloniex = function Poloniex() {};
Poloniex._query_tradeApi = function (req, callback) {
var post_data,
hash = crypto.createHmac('sha512', "key-key-key"),
sign,
received,
headers;
nonce = (new Date()).getTime() * 1000;
post_data = querystring.stringify(req);
hash.update(post_data);
sign = hash.digest("hex");
try {
headers = [ 'Key: ' + "SECRET-SECRET-SECRET", 'Sign: ' + sign ];
var curl = new Curl(),
close = curl.close.bind( curl );
curl.setopt('URL', 'https://poloniex.com/tradingApi/');
curl.setopt('POST', 1);
curl.setopt('POSTFIELDS', post_data);
curl.setopt('HTTPHEADER', headers);
received = '';
curl.on('data', function (chunk) {
received += chunk;
return chunk.length;
});
curl.on('header', function (chunk) {
return chunk.length;
});
curl.on('error', curl.close.bind( curl ),function (e) {
console.error('exchanges/poloniex', '_query_tradeApi', e,
req, e.stack);
callback(e, undefined);
curl.perform();
curl.close();
});
curl.on('end', function () {
try {
var data = JSON.parse(received);
callback(undefined, data);
} catch (ex) {
console.error('exchanges/poloniex', '_query_tradeApi',
ex, req, ex.stack);
callback(ex, received);
}
curl.close();
});
curl.perform();
} catch (ee) {
console.error('exchanges/poloniex', '_query_tradeApi', ee,
req, ee.stack);
callback(ee, received);
}
};
The syntax required is curl.setOpt, not curl.setopt.

handle https file response errors

How would I handle a situation where the file does not exist on the server, or is unable to connect with the server to get the file?
As of the code below, the file download still occurs, and the file is empty.
var https = require('https');
var fs = require('fs');
var exec = require('child_process').exec;
var file = fs.createWriteStream('mediaserver_setup.exe');
var len = 0;
var req = https.get(url + 'appdata/update_setup.exe', function (res) {
if (res.statusCode === 404) {
alert('problem with request:');
}
res.on('data', function (chunk) {
file.write(chunk);
len += chunk.length;
var percent = (len / res.headers['content-length']) * 100;
progressLoading.val(percent);
});
res.on('end', function () {
setTimeout(function () {
file.close();
}, 500);
});
file.on('close', function () {
win.close();
exec('update_setup.exe');
});
});
req.on('error', function (err) {
alert('problem with request: ');
});
Use the error event for requests, or check the status code of the response:
var https = require('https');
var req = https.get(options, function(res) {
// use res.statusCode
if (res.statusCode === 404) {
// example for checking 404 errors
}
});
req.on('error', function(err) {
// an error has occurred
});

Categories

Resources