What could this error be in Chrome console: "Uncaught SyntaxError: Unexpected token ':'"

Why am I getting this error in the Chrome console: "Uncaught SyntaxError: Unexpected token ':'"?

I have a JSON file located at the root of my application:

<script src="levels.json"></script>

Here is the content of my JSON file:

{
    "levels": [
        [22, 22],
        [33, 25]
    ]
}

Below is my JavaScript code:

let data = JSON.parse(levels)
console.log(data);

Answer №1

If you want to load a "JSON" file using a script tag, your "JSON" data must be formatted in JavaScript syntax. This is because the browser will attempt to parse your JSON as if it were JavaScript code.

Example JSON data:

{"name":"John", "age":31, "city":"New York"}

"JSON" in JavaScript syntax:

const data = {"name":"John", "age":31, "city":"New York"}

Once your "JSON" file is loaded, you will have a global variable named data available for use.

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

A guide on incorporating and utilizing third-party Cordova plugins in Ionic 5

Attempting to implement this plugin in my Ionic 5 application: https://www.npmjs.com/package/cordova-plugin-k-nfc-acr122u I have added the plugin using cordova plugin add cordova-plugin-k-nfc-acr122u but I am unsure of how to use it. The plugin declares: ...

Despite the correct value being displayed in the console.log, the Textfield is not responding to the Reducer

I am currently working on a project to create a tool that can iterate through the pupils of a school class. In order to achieve this, I have implemented a text field in a react component that displays a value: <input className="form-control" onChange={ ...

A comprehensive guide on extracting attachments and inline images from EML files and transforming them into HTML format

Our challenge is parsing .EML files to display full emails with inline images and attachments on a webpage. We have successfully extracted the HTML body using MimeKIT for .net, but we are unsure of how to distinguish between inline images and regular email ...

Create HTML content dynamically with JavaScript and then utilize AngularJS to interpret and render it

I am creating an HTML table that looks like this: <table id="#webappname"> <tr ng-repeat="data in d"> <td>1 {{data}}</td> <td>2 hey !</td> </tr> </table> To create this, I am using a ...

Problem encountered while parsing JSON through NSJSONSerialization

Having trouble parsing JSON data? Check out this code snippet: NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/lookup?bundleId=com.clickgamer.AngryBirds"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; ...

Parsing JSON in Swift

I am attempting to parse this JSON data ["Items": <__NSSingleObjectArrayI 0x61000001ec20>( { AccountBalance = 0; AlphabetType = 3; Description = "\U0631\U06cc\U0648"; FullCode = "P_21_JIM_456_IR_25"; IRNumber ...

NodeJs Importing a File

Currently working with NodeJS, I have encountered a challenge. Is it possible to require a JavaScript file in Node similar to how we do in browsers? When using the require() method, I noticed that the JavaScript file called does not have access to global v ...

Sending a POST request using PHP CURL with an empty value in the data

I have tried using this code to send data via POST request to a specific URL. The content-type header is set as text/html. file_get_contents("php://input"); The code appears to be successfully sending the data, however, the values are not being received a ...

How can I trigger the second animation for an object that is constantly moving within a specific range in the first animation?

I am looking to create a simulation of rising and falling sea levels. Currently, when I click the button, the level rises from its starting point to the top, but I am unsure how to achieve this effect in my code. Below is the code I have written so far, an ...

Unable to interpret encoded json information

I'm currently developing a basic chat system using PHP and jQuery with Ajax, but I've encountered an issue when trying to display a JSON encoded string through the ajax callback function. Below is the code for the ajax request: $.ajax({ url: ...

AngularJS: ng-show causing flickering issue upon page refresh

Recently, I encountered an issue with my code snippet: <body> <ng-view></ng-view> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script> <script src="http://ajax.googleapis.com/ajax/ ...

The code functions correctly on JSfiddle, however it is not executing properly on my website

When I tried to implement the code from this jsfiddle link: http://jsfiddle.net/mppcb/1/ on my website (), it didn't work as expected: Here is the HTML code snippet: <form id="myform" novalidate="novalidate"> <input type="text" name="fi ...

What is the process for importing a sprite sheet along with its JSON file into my Phaser game?

Currently, I am in the process of developing a game using the powerful phaser game engine. To enhance the visual appeal of my game, I decided to create a sprite sheet and successfully downloaded it. The sprite sheet consists of a 256 × 384 .png file ...

Issues with voice state updates in JavaScript

I am currently working on setting up a discord bot to send notifications when someone joins a voice chat. While coding in Atom, I encountered the following error: TypeError: Cannot read property 'send' of undefined at Client.client.on (C:\U ...

Fetching JSON array from servlet with AJAX

I have two JSON arrays in my servlet. I am trying to retrieve the values from the JSON arrays and display them on a JSP page using AJAX. Here is the code snippet: JSONArray htags = new JSONArray(); htags.add("#abc"); htags.add("#xyz"); ...

Troubleshooting Problem with Bootstrap CSS Menu Box Format

I'm having trouble creating a simple menu for my Bootstrap site. What I want to achieve is something like this: This is what I have accomplished so far: I've attempted to write the CSS code but it's not working as expected. Below is the ...

Discover the method for measuring the size of a dynamically generated list

I'm currently working on a chat box that displays messages as a list. One issue I'm facing is that when I click on the start chat button, the chat box opens but the length of the list always remains zero. How can I resolve this problem? $(docu ...

Preserve user-inputted text from jQuery within a div container

With the help of the knowledgeable individuals here, I successfully created a prototype to address an issue I had encountered. The problem involved using a textbox input with a password requirement to update an HTML element. Although everything is functio ...

Firefox compatibility issue with Angular JS for downloading excel files

I've encountered an issue with my AngularJS code for downloading an Excel file. While it works smoothly in Chrome and Internet Explorer, it fails to function in Firefox. The server response isn't presenting any problems. Here's the code snip ...

Please display the Bootstrap Modal first before continuing

Currently, I'm facing a challenge with my JS code as it seems to continue running before displaying my Bootstrap Modal. On this website, users are required to input information and upon pressing the Save button, a function called "passTimeToSpring()" ...