1

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

2
  • Have you inspected the response to make sure it's actually returning JSON? Commented Sep 22, 2017 at 10:46
  • It doesn't return JSON, I tried to JSON.parse(result) in parser but still no effect Commented Sep 22, 2017 at 10:49

2 Answers 2

2

You angular service is calling './api/course-list' which is not a valid url. And, probably you have configured your server to return index.html page for even 404 page. That's why your angular client might be getting html page, and throwing the error while parsing it into `

Hope this._http.get('/api/course-list') fixes the issue.

Sign up to request clarification or add additional context in comments.

5 Comments

I have proxy setup which replaces .api with proper path. I am definitely calling endpoint properly as I have other endpoints which work. The problem is with data I am sending back to the angular
Check Chrome Debugger Console's Network tab to see the response you get from backend. Unexpected token < in JSON at position 0 clearly says that the response starts with <, and it is possible in case of html/xml. Since you have verified the api in Postman, the only possibility left is related to angular-client only.
I am getting response Status Code:200 OK, content type is not ok content-type:text/html; charset=UTF-8
And, check the content as well. It should not be json.
Found solution, part of the problem was the path as in Node.js I had /courselist and in angular it was /course-list.But I still needed to make more tweaks. Thanks for your help!
2

I managed to find a solution I changed parsing to be response.body instead of body and it formatted XML properly. Additionally paths in node.js and angular were not same.

router.get('/course-list',  (req, res, next) => {
    request("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml", function(error, response, body) {
        var parsedBody;
        var doneParsing = false;
        parser(response.body, function (err, result) {
            parsedBody = result;
            doneParsing = true;
        });
        if (doneParsing === true) {
            response.body = parsedBody;
        }
        res.json(response);
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.