0

I am re-writing an existing js app into angular and ts. I have a json data file that I read as part of the startup of the existing js app and it works fine. When I try and read the exact same file with the exact same line of code in angular, ts/angular barfs all over it. Here is the code from the existing app:

    aircraftData = JSON.parse(data);

and the console log is empty. Here is the the code from the new angular version of the app:

    const content = require('./shared/imeiXref.json');  
    console.log("IMEI=",content[0].IMEI);

and the console output:

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
.....

Is JSON expected to be in a different format from js to angular/ts?

Thanks....

cpeddie
  • 759
  • 13
  • 38
  • Try using the `data` without using `JSON.parse()` – dileepkumar jami Feb 23 '19 at 08:37
  • This might help [How to import JSON File into a TypeScript file?](https://stackoverflow.com/questions/46991237/how-to-import-json-file-into-a-typescript-file) – Vadi Feb 23 '19 at 08:40
  • Here is various ways to get data from file: https://stackoverflow.com/questions/47206924/angular-5-service-to-read-local-json-file – Sanjay Feb 23 '19 at 08:45
  • It was actually already working. I didn't realize that when I read the file in I was already able to parse the JSON by field name so I don't even need to run JSON.parse. – cpeddie Feb 24 '19 at 16:25

1 Answers1

0

It was actually already working. I didn't realize that when I read the file in I was already able to parse the JSON by field name so I don't even need to run JSON.parse.

cpeddie
  • 759
  • 13
  • 38