Parsing error: 'Unexpected token' found in JSON while attempting to access an external file

Currently working on implementing backbone for a new project along with underscore, requirejs, jquery, and bootstrap. Things are progressing smoothly as I aim to include static survey question data into one of the 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": {}
        }
    }
}

Currently without access to the final data API intended for the project, using dummy data extracted from the previous version related to the US Elections cycle which occurs annually. Separating out some static content model data into a different file to prevent accidental alterations. Working within the same server constraints and processing JSON through for validation.

Encountering an error in Chrome "Uncaught SyntaxError: Unexpected token : on line 2" and FireFox displays 'SyntaxError: missing ; before statement "defaultOptions":', focusing on the : following defaultOptions.

Calling two json files consecutively:

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

No issues with endorsements.json file or other required contents. Referencing endorsements.json below:

[
    "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"
]

Any insights on resolving the issue would be greatly appreciated.

Answer №1

When you use the require() function in RequireJS, it expects an AMD module to be called. To load text resources like JSON, it is recommended to utilize the RequireJS text plugin. Additionally, there is a useful plugin available on this GitHub gist for parsing JSON specifically, which is built upon the text plugin.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transform the string into a file format, followed by encoding it into base64

Is there a way to take the string const content="<div>How do I <b>convert </b> this string to file?</div>"; and convert it into an HTML file, then ultimately encode it as a base64 string? The method Buffer.from(content).toString(&ap ...

Guide on programmatically changing the position of a shape in SVG

Check out this tutorial on how to make SVG elements draggable using JQuery and Jquery-svg. The accepted answer provides an implementation for moving a circle by changing the cx and cy attributes. But how can I achieve drag-and-drop functionality for a pol ...

Issue with form validation in Bootstrap 5.2 JavaScript implementation

I'm having trouble with my client-side form validation not working properly before the server-side validation kicks in. I've implemented Bootstrap 5.2 and followed the documentation by adding needs-validation and novalidate to the form. Scenario ...

Functionality of jQuery Mobile Page

$(document).on("pageshow", "#mappage", function (event) { $("#doldur").append("<li> <label onclick='getBina(" + featuressokak[f].attributes.SOKAKID + ");'>" ...

Retrieve a single document from Firestore and assign it to a variable

While I'm still new to NodeJS, I'm currently working on retrieving a single User document from Firestore. const fs = firebase.firestore(); const usersRef = fs.collection('users'); let findUserByContact = (contact) => { let res ...

Leveraging the power of react routes for efficient navigation within a react-based application

I am currently facing an issue with setting up routes for a basic react application using react-router. The given routes don't seem to match and the switch defaults to displaying the 404 page. Below is the code for the routes: import { BrowserRout ...

Sharing information in React applications

I'm a beginner when it comes to using javascript and React, so I have a question regarding posting data from within a component. In my scenario, I am utilizing the fetch API for making POST requests. Below is the code snippet I have created: export f ...

Trigger the opening of a bootstrap modal from an external source on the current page

One common question is how to load a modal from another page onto the current page, or how to open a modal on the current page when it loads. My idea is a little different: I want to click on an image hotspot (like a person in a team photo) on the /home p ...

Clicking on Bootstrap Select Does Not Trigger Dropdown Opening

Currently, I am working on generating a dynamic set of bootstrap-select widgets that include a dropdown. My main challenge lies in preventing the select from closing the dropdown, which requires me to use 'event.stopPropagation()' on the dropdown ...

Issue with Javascript variables

In Javascript, I have an array of strings that I need to use for loading images on my page through AJAX. After each image is loaded, there are additional tasks to be performed which include sending a HTTP request to delete the image. Below is the code I c ...

Dynamic Node name in SSIS Kingswaysoft Json Source

Although I am well-versed in SSIS development, I am completely new to the Kingswaysoft Json Source Editor (and all other Kingswaysoft SSIS tools!) I have numerous API sources that share a very similar structure: { "values": { "XXX": ...

Utilize Function to Gain Access to React Context

As I work on developing a package that enhances the responsiveness of my React widget, I have encountered an issue. The responsiveness now relies not on the viewport width, but rather on the width of the widget container element. Presently, I am wrapping ...

Having trouble with JSON parsing on iOS?

I'm facing an issue with parsing a JSON response in the code snippet provided below. The JSON response format is as follows: {"response":"session_exists"} Here is the code I am using to echo the JSON: $json_output = array('response' => ...

Verify modifications prior to navigating in React or Next.js

I have a simple Next JS application with two pages. -> Home page import Header from "../components/header"; const handleForm = () => { console.log("trigger"); }; export default () => ( <> <Header /> & ...

Tips for saving ajax response data in the browser's cache

I need to send a large amount of configurations to the browser using an ajax request. I want to store these objects in the browser cache, not HTML5 local storage. The data amounts to over 5mb and needs to be stored. Is there a way to set this data using ...

Choose the option for overseeing safaris

Hello there! I need some help with Safari. Can you please guide me on how to disable the arrows? https://i.stack.imgur.com/1gzat.png ...

I'm trying to find a way to access a particular field within an HTML document using JavaScript in Node.js. Can anyone

<Response> <SMSMessageData> <Message>Delivered to 1/1 Total Cost: NGN 2.2000</Message> <Recipients> <Recipient> <number>+9109199282928</number> <cost>NGN 2.2000&l ...

configure dynamic content within the slider element

Currently, I am experimenting with two jQuery plugins (awkward/Coda Slider 3) to implement a sliding effect for DIV content. Everything seems to be working smoothly until I attempt to set dynamic content (using JavaScript) after creating the plugin object. ...

Angular 6 throws an error stating: "The property 'push' cannot be read of null."

The Cart model is defined as follows: //declaring a cart model for products to add export class Cart{ id:string; name:string; quantity:number; picture:string; } Below is the code for my app.service.ts: import { Injectable, Inject } fro ...

Generate radio buttons in a model loop in Vue.js based on names automatically

I am attempting to generate a model for each radio button using the inputname as the identifier. My approach involves looping through the array of objects to identify instances where more than one inputname is present, and then converting this value into a ...