I recently ran into a problem while attempting to export data to a file. Once the backend had returned the data, I received an error like the following: Unexpected Token X in JSON at position N at JSON.parse.... The solution that worked for me in Angular is as follows:

When using the httpClient in Angular, add the following when making the AJAX call to the endpoint:

getFile(): Observable<any> {

return this.http.get(myURL, {responseType: 'blob'});
}

Simply substitute your URL for myURL and add {responseType: 'blob'}. Your data should now output without the error message above. In terms of the return type for the function, it is not needed to state that the return type is a blob, since that is explicitly stated as the response type within the HTTP call.