Lisa
Lisa

Reputation: 2139

'Unexpected Token' in json when calling an external file

I'm trying to implement backbone for a new project. Also installed are underscore, requirejs, jquery and bootstrap. Overall it is going well. I want to incorporate some static survey question data into one of my data models.

{
    "defaultOptions": {
        "1": "Strongly Agree", 
        "2": "Somewhat Agree",
        "3": "Have Mixed Feelings",
        "4": "Somewhat Disagree",
        "5": "Strongly Disagree",
        "6": "Have No Opinion",
        "7": "Do Not Wish To Respond"
    },
    "questions": {
        "1": {
            "question": "Question 1",
            "options": {}
        },
        "2": {
            "question": "Question 2",
            "options": {}
        },
        "3": {
            "question": "Question 3",
            "options": {}
        },
        "4": {
            "question": "Question 4",
            "options": {}
        },
        "5": {
            "question": "Question 5",
            "options": {}
        }
    }
}

I don't currently have access to the data API I will ultimately be using to build this project, so I'm currently working off of some dummy data exported from the previous iteration of this project (It's for the US Elections cycle, so it happens yearly). I'm trying to pull some static data out of the content model I'm using and put it into a separate file so it's out of my way and I don't have to worry about inadvertently messing it up. No big deal, right? I'm not crossing any domains, it all lives on the same server. I have another data set doing the same thing. I even ran the information through http://www.jsoneditoronline.org/ to make sure it was valid json.

So what's the problem? I get this error in chrome "Uncaught SyntaxError: Unexpected token : on line 2" and this error in FireFox 'SyntaxError: missing ; before statement "defaultOptions":', with the indicator pointing towards the : after defaultOptions.

I really have no idea why this is happening.

I am calling two json files, one right after the other:

var survey = require('/elections/data/surveyQuestions.json');
var endorsements = require('/elections/data/endorsements.json');

The endorsements.json file (and anything else called with require) doesn't have any issues. Here it is for reference:

[
    "Abortion Rights Council",
    "AFL-CIO",
    "AFSCME",
    "American Federation of Teachers",
    "Building and Construction Trades Council",
    "DFL Feminist Caucus",
    "DFL Party",
    "Education Minnesota",
    "Freedom Club",
    "GOP Feminist Caucus",
    "Grassroots Party",
    "Green Party of Minnesota",
    "Independence Party",
    "Libertarian Party of Minnesota",
    "MAPE",
    "Minnesota Citizens Concerned for Life",
    "Minnesota Police and Peace Officers Association",
    "National Association of Social Workers",
    "Republican Party of Minnesota",
    "Sierra Club",
    "Stonewall DFL",
    "TakeAction Minnesota",
    "Taxpayers League of Minnesota",
    "Teamsters DRIVE",
    "United Auto Workers"
]

Does anybody have any thoughts as to what I'm missing?

Upvotes: 0

Views: 303

Answers (1)

Herman Tran
Herman Tran

Reputation: 1591

RequireJS is expecting an AMD module when you call require(). You should use the RequireJS text plugin for loading text resources like JSON. Also look at this GitHub gist for a plugin to parse JSON specifically that's built on top of the text plugin.

Upvotes: 2

Related Questions