Currently, I am in the process of converting an existing JavaScript application into Angular using TypeScript. In the original JavaScript application, I have a JSON data file that is successfully read during startup. However, when attempting to read the exact same file using Angular with TypeScript, I encounter errors. Here is the code snippet from the original app:
aircraftData = JSON.parse(data);
When I run this code, the console log outputs nothing. Now, here is the code snippet from the new Angular version of the application:
const content = require('./shared/imeiXref.json');
console.log("IMEI=",content[0].IMEI);
Upon running this code, the console displays the following error:
AppComponent_Host.ngfactory.js? [sm]:1 ERROR SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at AppComponent.push../src/app/app.component.ts.AppComponent.ngOnInit (app.component.ts:33)
at checkAndUpdateDirectiveInline (core.js:22089)
at checkAndUpdateNodeInline (core.js:23353)
at checkAndUpdateNode (core.js:23315)
at debugCheckAndUpdateNode (core.js:23949)
at debugCheckDirectivesFn (core.js:23909)
at Object.eval [as updateDirectives] (AppComponent_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:23901)
at checkAndUpdateView (core.js:23297)
View_AppComponent_Host_0 @ AppComponent_Host.ngfactory.js? [sm]:1
proxyClass @ compiler.js:18234
push../node_modules/@angular/core/fesm5/core.js.DebugContext_.logError @
core.js:24129
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:15762
(anonymous) @ core.js:18116
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @
zone.js:391
.....
This raises the question: Is the JSON format expected to be different when working with JavaScript compared to Angular/TypeScript?
Thank you for your help.