Deepak3301086
Deepak3301086

Reputation: 487

Angularjs:SyntaxError: Unexpected token ' in JSON at position 97 at JSON.parse

This is my output

{"contacts":[{"id":"1","company":"Cutting","contact":"Russell Chimera"},{"id":"2","company":"Gge\'s","contact":"Marci"}]}

I got this error

SyntaxError: Unexpected token ' in JSON at position 97 at JSON.parse

I tried to print it like {{x.company}} or {{x.company | escape}} but problem remain same. I try to print company name "Gge's". I don't want to remove single quote from company name.
Can anyone please help me to find solution?

Upvotes: 3

Views: 2328

Answers (4)

In my case I just removed commented lines in JSON file(post in postman) as JSON won't take comments in them.

Upvotes: 0

Shashaank V V
Shashaank V V

Reputation: 740

You must know that the JSON key "contacts", has a JSON array as its value. to access your value "Gge\'s", you will have to access,

x = data.contacts[1].company;

where your data is :

data = {"contacts":[{"id":"1","company":"Cutting","contact":"Russell Chimera"},{"id":"2","company":"Gge\'s","contact":"Marci"}]}

Upvotes: 4

Varun Srivastawa
Varun Srivastawa

Reputation: 72

Please check this :"Gge\'s" . Please try to put unicode of ' this then try to parse it then it will resolve the problem.

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Your scope variable x should hold the value of data.contacts[1], to get the value of Gge's with single quote from company name. There is no such scenario in AngularJS where it omits the single quote from the JSON value. Be sure of your JSON again.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   var data = {
    "contacts":[
     {
       "id":"1","company":"Cutting","contact":"Russell Chimera"
     },
     {
       "id":"2","company":"Gge\'s","contact":"Marci"
     }
    ]
   };
   $scope.x = data.contacts[1];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  {{x.company}}
</div>

Upvotes: 2

Related Questions