5

The following is the JSON I'm trying to parse:

{[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}

I'm getting the error below:

Unexpected token [ in JSON at position 1
    at JSON.parse (<anonymous>)
    at fromJson

I'm getting the data from a service and saving it into the controller scope using the following:

vm = this;
vm.sectorList = response.data;

And my HTML:

<div ng-repeat="sector in ctrl.sectorList">
    {{sector.name}}
</div>
3
  • This is not a valid JSON document. Remove the wrapping curly braces and you are good to go. Commented Jan 31, 2017 at 15:21
  • JSON is not properly formatted, should be this {keyname:[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]} Commented Jan 31, 2017 at 15:21
  • The code in the service, where you handle the response, is probably more interesting than your controller. It would be helpful to include it in your question. Commented Jan 31, 2017 at 15:35

6 Answers 6

11

The JSON is invalid, hence the error you're getting. You're missing a key to go with the JSON array value. This for instance (key "anything" added) will work:

{"anything": [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}
Sign up to request clarification or add additional context in comments.

Comments

6

If the response is an array it should not be enclosed in curly brackets {}. This would be valid JSON:

[{"name":"Technology"}, {"name":"Engineering"}, {"name":"Business"}]

You are also trying to use JSON.parse on an object, when it takes a string as input parameter. If you want to make an object into a JSON string you should use JSON.stringify.

Comments

5

Your JSON is invalid you can validate your JSON Here

It should be:

[{
    "name": "Technology"
}, {
    "name": "Engineering"
}, {
    "name": "Business"
}]

1 Comment

Thank you for the link to The JSON Validator
3

You should remove the curly braces before the [] in your JSON :

[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]

Comments

3

Your key is missing.

{"YourKEY": [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}

You cannot access object of arrays without key.

Comments

3

you need to assign the array to property of the object. can't assign array just inside of the object without property

{  
   "items":[  
      {  
         "name":"Technology"
      },
      {  
         "name":"Engineering"
      },
      {  
         "name":"Business"
      }
   ]
}

in the ng-repeat it should change as following

<div ng-repeat="sector in ctrl.sectorList.items">
    {{sector.name}}
</div>

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.