I am trying to convert xml data to JSON in order to return it to my Angular app.
I've been able to get data but I'm not sure how to convert and return to Angular.
I am using xml2js parser plugin to convert xml.
node.js
router.get('/courselist', (req, res, next) => {
request("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml", function(error, response, body) {
console.log(body);
parser(body, function (err, result) {
res.json(response);
});
});
After parsing the output is like this:
{"gesmes:Envelope": {
"$": {
"xmlns:gesmes": "http://www.gesmes.org/xml/2002-08-01",
"xmlns": "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"
},
"gesmes:subject": [
"Reference rates"
],
"gesmes:Sender": [
{
"gesmes:name": [
"European Central Bank"
]
}
],
"Cube": [
{
"Cube": [
{
"$": {
"time": "2017-09-21"
},
"Cube": [
{
"$": {
"currency": "USD",
"rate": "1.1905"
}
},
....
{
"$": {
"currency": "JPY",
"rate": "133.86"
}
},
]
}
]
}
]
}
}
Angular service
getCourseList() {
return this._http.get('./api/course-list').map(
(res: Response) => res.json()
).catch(this.handleError);
}
When I call the endpoint in Postman I can see parsed output, but in Angular I am getting error as I am not returning JSON object.
Unexpected token < in JSON at position 0
I've been looking around SO for a solution but not been able to find any which would suit me. Can you please advise what am I doing wrong as I am beginner with Node.js
JSON.parse(result)in parser but still no effect