3

Am newbie to Json am getting this error while call json my json file expected ',' or ']'

[
{
    "modules":
        [
                {
                   "title":"name of module1",
                   "description":"description of module1",
                   "weeks":[{"id":1, "title":"Week 01"}]
                },

                {
                   "title":"name of module2",
                   "description":"description of module2",
                   "weeks":[{"id":2, "title":"Week 02"},{"id":3,"title":"Week 03"}]
                }
        ]
  },
{

    "products":
      [
        {
          "url":"http://dummyimage.com/242x200/f3f3f3/2d2d2d.png",
          "description":"Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.",
          "viewurl" : "/",
          "savebtn" : "save"
        },
        {
          "url":"http://dummyimage.com/242x200/f3f3f3/2d2d2d.png",
          "description":"Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.",
          "viewurl" : "/",
          "savebtn" : "save"
        }
      ]
}

]

hope i did correct i dont know what is happening its getting Error: JSON.parse: expected ',' or ']' after array element am attempting to use in angular js controller

app.controller('settingsController', function($scope, $http){
    $http.get('assets/javascripts/datas.json').then(function(result){
        $scope.employe = result.data;
        $scope.prod = result
    })
});

and in view

 <div class="col-sm-6 col-md-4" ng-repeat="datas in prod.products">
            <div class="thumbnail">
                <img ng-src="{{ datas.url }}" alt="product">
                <div class="caption">
                   <h3>{{ datas.caption}} </h3>
                   <p>{{ datas.description}}</p>
                   <p><a role="button" class="btn btn-primary" ng-href="{{datas.viewurl}}">Button</a> <a role="button" class="btn btn-default" href="#">{{datas.savebtn}}</a></p>
                </div>
            </div>
        </div>

and the error in console

Error: JSON.parse: expected ',' or ']' after array element at line 76 column 9 of the JSON data cc@/test/assets/javascripts/vendors/angular.js:14:289 Ud/this.defaults.transformResponse<@/test/assets/javascripts/vendors/angular.js:69:58 xc/<@/test/assets/javascripts/vendors/angular.js:68:352 r@/test/assets/javascripts/vendors/angular.js:7:288 xc@/test/assets/javascripts/vendors/angular.js:68:336 b@/test/assets/javascripts/vendors/angular.js:70:65 ye/e/l.promise.then/K@/test/assets/javascripts/vendors/angular.js:100:59 ye/f/<.then/<@/test/assets/javascripts/vendors/angular.js:101:235 Zd/this.$get

7
  • Hmm, JSONLint.com says it's all good. Can you show your code around where you're attempting to use JSON.parse? Commented Sep 5, 2014 at 5:35
  • Is there more to that error message like a stack trace? Commented Sep 5, 2014 at 5:40
  • @Phil yup added my error stack Commented Sep 5, 2014 at 5:43
  • Have you perhaps registered a response transformer with the $httpProvider? If so, what does it look like? Commented Sep 5, 2014 at 5:46
  • 1
    Sorry, I'm not sure what "using 'ngRoute'" means. Also, is the JSON block at the top of your question the entire contents of assets/javascripts/datas.json? Check your browser's Network developer console and inspect the server response for that file to make sure it matches exactly Commented Sep 5, 2014 at 5:52

3 Answers 3

2

This error occurred for me when I parsed JSON from a string literal. When I copied in the JSON, I forgot that the escape sequences would need to be double escaped.

Example of the problem:

var data = JSON.parse('[{"key": "This value has \"a quoted\" string"}]');

While the string value is valid JSON, the escape sequences are lost. Then need to be double escaped:

Corrected version:

var data = JSON.parse('[{"key": "This value has \\\"a quoted\\\" string"}]');
Sign up to request clarification or add additional context in comments.

Comments

0

Well for me the problem occurs if I have a :// (e.g. http://) inside my json stirng for this reason I replaced it and it worked.

Comments

0

A better way I found to get rid of this issue in javascript is:

JSON.parse(JSON.stringify(String.raw`${your_string}`))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.