2

I'm trying to make a simple GET request to a JSON file located within my project directory and keep receiving the following: SyntaxError: Unexpected token ]

I don't see any syntactical issues with my JavaScript or JSON. The path to the JSON is correct gives me a 304 response. My JavaScript and JSON are fairly straight-forward:

JavaScript:

// app
var app = angular.module('app', []);

// controllers
app.controller('myController', function($scope, $http){

  $scope.data = null;

  $http.get('data.json').success(function(data){

    $scope.data = data;
    console.log($scope.data);
  });
});

JSON:

{
 "data": [
  {
    "title": "Test 1",
    "description": "Fusce vulputate eleifend sapien."
  },
  {
    "title": "Test 2",
    "description": "Vivamus laoreet."
  },
  {
    "title": "Test 3",
    "description": "Quisque ut nisi."
  },
 ]
}

What is preventing me from retrieving the data from within my JSON file?

1
  • 2
    it is not a valid json, there is an extra comma after the last element in data array Commented Apr 26, 2016 at 21:09

2 Answers 2

3

You have an extra comma at the end of your JSON:

{
 "data": [
  {
    "title": "Test 1",
    "description": "Fusce vulputate eleifend sapien."
  },
  {
    "title": "Test 2",
    "description": "Vivamus laoreet."
  },
  {
    "title": "Test 3",
    "description": "Quisque ut nisi."
  }, <<<< HERE
 ]
}
Sign up to request clarification or add additional context in comments.

Comments

2

Normally, in JavaScript, adding an extra comma to an array/object won't cause any problems (in the code editor, for example), even though it's not best practice.

But since you're using this JSON in a request, the JSON syntax must be correct.

Removed a comma at the end of the array containing objects.

{
 "data": [
  {
    "title": "Test 1",
    "description": "Fusce vulputate eleifend sapien."
  },
  {
    "title": "Test 2",
    "description": "Vivamus laoreet."
  },
  {
    "title": "Test 3",
    "description": "Quisque ut nisi."
  }
 ]
}

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.