Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: JSON Parse error: Property name must be a string literal when using angular translate

I use nodejs & expressjs for my backend, Angular for my front-end. When using Angular translate in my site, the console shows this message:

Error: JSON Parse error: Property name must be a string literal
parse@[native code]
fromJson@http://localhost:3000/angular/angular.js:1321:19
defaultHttpResponseTransform@http://localhost:3000/angular/angular.js:10393:24
http://localhost:3000/angular/angular.js:10484:14
forEach@http://localhost:3000/angular/angular.js:322:24
transformData@http://localhost:3000/angular/angular.js:10483:10
transformResponse@http://localhost:3000/angular/angular.js:11278:34
processQueue@http://localhost:3000/angular/angular.js:15961:30
http://localhost:3000/angular/angular.js:15977:39
$eval@http://localhost:3000/angular/angular.js:17229:28
$digest@http://localhost:3000/angular/angular.js:17045:36
$apply@http://localhost:3000/angular/angular.js:17337:31
done@http://localhost:3000/angular/angular.js:11572:53
completeRequest@http://localhost:3000/angular/angular.js:11778:15
requestLoaded@http://localhost:3000/angular/angular.js:11711:24

I am sure that all properties in JSON files are quoted.

This is the locale files:

{
  "directives.language-select.Language": "Select Language",
  "views.video-modal.Download": "Download",
  "views.video-modal.Visit": "Visit",
  "views.video-modal.Copy": "Copy",
  "views.video-modal.Close": "Close",
}

Strangely, when I only had

"directives.language-select.Language": "Select Language",

this line. The site worked perfectly. What causes this problem?

like image 229
Joshua Leung Avatar asked Aug 24 '26 16:08

Joshua Leung


2 Answers

Ok, a thought just came up and I found out that you are not supposed to put ',' at the end of the very last line. Deleting the ',' solved my problem.

For those who encounter this problem, you may try this.

like image 75
Joshua Leung Avatar answered Aug 27 '26 06:08

Joshua Leung


I just running into this problem when try to return a JSON back from the PHP server. And turn out JSON does not allow trailing comma. Stating in the JSON Specification

like image 35
Brody Avatar answered Aug 27 '26 07:08

Brody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!