I'm trying to read the JSON string that is inside the <pre> element here:
http://nlp.stanford.edu:8080/corenlp/process?input=hello%20world&outputFormat=json
If I copy-paste the string with the mouse, I can JSON.parse() it. But if I read it programmatically, I get an error.
Here is my code:
var request = require('request'); // to make POST requests
var Entities = require('html-entities').AllHtmlEntities; // to decode the json string (i.e. get rid of nbsp and quot's)
var fs = require('fs')
// Set the headers
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
}
// Configure the request
var options = {
url: 'http://nlp.stanford.edu:8080/corenlp/process',
method: 'POST',
headers: headers,
form: {
'input': 'hello world',
'outputFormat': 'json'
}
}
// Start the request
request(options, function(error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
console.log("body: " + body)
let cheerio = require('cheerio')
let $ = cheerio.load(body)
var inside = $('pre').text();
inside = Entities.decode(inside.toString());
//console.log("inside "+ inside);
var obj = JSON.parse(inside);
console.log(obj);
}
})
But I get the following error:
undefined:2
"sentences": [
^
SyntaxError: Unexpected token in JSON at position 2
at JSON.parse (<anonymous>)
And here is an excerpt from the output of the link, i.e. what I want to parse into obj:
{
"sentences": [
{
"index": "0",
...
}
]
}
How can I JSON.parse() such a string?
Thanks,
JSON.parseshouldn't care about whitespace...{is diagnosed as an unexpected token.triming the string prior to parsing to remove the leading whitespace. It shouldn't matter, but worth trying since the error is pointing you to that spot.