JSON at position 2 throws an error with an unexpected I token

Take a look at this code:

<script>
try {
    var jsonObject = JSON.parse("{ ID: 1, 'Code':'001', 'Name':'john', 'HasParent':false, 'HasGrandParent':false, 'IsAgent':False }");
    document.write(jsonObject.Name);
}
catch(error) {
    document.write(error);
}
</script>

Here is the error message:

Unexpected token I in JSON at position 2

I'm confused about what's causing this. Can anyone help?

==================

Big thanks to JayTheKay for the helpful response.

Here are the issues and solutions:

  1. Strings should be enclosed in double quotes
  2. Boolean values should be lowercase, so "False" should be "false"
  3. The first key, ID, should not be surrounded by double quotes

Answer №1

Remember to always use double quotes instead of single quotes when enclosing JSON properties. For reference, you can check out this related question:

It seems like your first property is missing the required quotes, which could be the reason for the error you are experiencing.

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 retrieving a JSON response from a servlet and displaying it on a JSP page using jQuery Ajax

I am currently working on retrieving a JSON response that has been established within a servlet to be displayed on a JSP page. JSP page: <script> $(document).ready(function(){ $("#submitBut").click(function(){ var formData=ge ...

jwplayer - track viewing time - monetize by the minute - trigger action based on duration

My goal is to track the time duration that someone watches a video, ideally by triggering an action every minute. I'm aiming to create a pay-per-minute system where a credit is withdrawn from the user for each minute they watch. If this setup isn&apo ...

``What methods can I use to split JSON data using logstash and visualize it in Kibana?

I am facing an issue with parsing a log file containing multiple lines of JSON data. One line from the log looks like this: {"name":"sampleApplicationName","hostname":"sampleHostName","pid":000000,"AppModule":"sampleAppModuleName","msg":"testMessage","tim ...

Ways to Retrieve JavaScript Variable inside HTML Tags in a JSP

I am currently facing a requirement where I must assign js variables to html input tag ids. For example: <input type='text' id='(here I need js variable)'/> I am aware that one way to achieve this is by creating the entire elem ...

Generating fresh Mongodb Records versus Appending to a Document's Collection

A device that records temperature data every second feeds into a real-time chart using Meteor.js to display the average temperature over the past 5 seconds. Should each temperature reading be saved as a separate MongoDB document, or should new readings be ...

Is it possible to create my TypeORM entities in TypeScript even though my application is written in JavaScript?

While I find it easier to write typeorm entities in TypeScript format, my entire application is written in JavaScript. Even though both languages compile the same way, I'm wondering if this mixed approach could potentially lead to any issues. Thank yo ...

Tips for properly sending a JWT token

When working on my angular application, I encountered an issue while trying to send a jwt token as a header for request authorization. The error 500 was triggered due to the jwt token being sent in an incorrect format. Here is how I am currently sending it ...

Using Django URL regex to handle JSON data

I seem to have hit a roadblock in my understanding of json queries. I am attempting to display json data on a view that has the URL structure provided below. http://localhost:8000/structures/hydrants/json?id=%3D2/ This is the regex I am using for the URL ...

Encountering the java.lang.NoClassDefFoundError error is a common issue that arises specifically in

Currently, I am successfully using protobufs with the play framework 2.1.3. However, I encountered a need to convert the protobufs to JSON, which prompted me to include "com.googlecode.protobuf-java-format" % "protobuf-java-format" % "1.2" in Build.scala ...

Encountering a problem in a NextJS application where an error occurs stating "cannot read property of undefined (reading setState)" within

In an attempt to resolve the issue, I have linked the method to this inside the constructor. Additionally, I have utilized the arrow function technique to address the problem at hand. Despite these efforts, I continue to encounter the following error: Unha ...

What is the best way to anchor the components to a specific location on the screen in ASP.NET?

Currently I am working on creating a registration page in asp.net. I have been using panels to group the components such as labels, dropdown lists, and text boxes. However, when I run the page, I noticed that the positions of these components keep changing ...

Implement custom styles using media queries within ngView to ensure compatibility with IE browsers even after a page

Experiencing issues with CSS rules in IE10 when included in a page via ngView (using Angular 1.0.6). Works fine in other browsers (potentially IE11, not personally confirmed): Here is the CSS code: .checker { display: none; } @media (min-width: 1041 ...

Trying to figure out how to avoid HTML characters in AngularJS? I've come across suggestions to use ng-bind / ng-sanitize for escaping HTML, but I'm struggling to put

As a newcomer to Angular Js, I recently utilized ngx-editor to create a text-area with various formatting styles. However, upon submitting the content, I noticed that HTML tags were also being displayed. I understand that these tags appear when using diffe ...

Is there a way to confirm that a file has been chosen for uploading prior to form submission, by utilizing the jquery validator?

I have a section on my website where users can upload files. I am trying to implement validation to ensure that a file has been selected before the form is submitted. Currently, I am using the jQuery form validator plugin for handling form validations. Th ...

Organizing a set of columns under a fresh header in the JSON serialization process

My dataset in Pandas consists of the following details: start end compDepth compReleaseDepth compMeanRate 0 0.0 0.62 58.0999985 1.5 110 1 0.66 1.34 57.1399994 3 94 2 ...

Managing Data Types in AJAX Requests

Can you help me figure out why my AJAX call is not reaching success after hours of troubleshooting? It seems like the issue lies in the dataType that the AJAX call is expecting or receiving (JavaScript vs JSON). Unfortunately, I'm not sure how to addr ...

What is the best way to display data in the User Interface when data is being received through the console in AngularJS?

I have created an HTML file and the corresponding controller logic for this page. I can see the data in the console, but it's not displaying on my UI. <div id="panelDemo14" class="panel panel-default" ng-controller="NoticeController"> < ...

Retrieve a file from an Express API using React with the help of Axios

When working with a React client and an Express API, how can the React client download a file sent by the Express API? Issue: If I manually enter the URL into my browser's address bar and hit enter, the file downloads successfully. However, when I ...

Unable to pass Ajax value to Laravel controller

Currently, I am facing an issue while trying to retrieve a value from an ajax request in my controller. Although my JavaScript function successfully displays the desired value in an alert, when I attempt to pass this value as data to the controller, it is ...

How can we export data to excel using react-export-excel while ensuring certain columns are hidden?

Greetings! I believe the title gives you a clear idea of my dilemma. When exporting data, there are no errors - my goal is to hide the Excel column when the checkbox is unchecked or false, and display it when the checkbox is checked or true during export. ...