The JSON.parse function encountered an Uncaught SyntaxError due to an unexpected token 'o

I'm struggling with this JSON data:

const info =  [{
    "ID":1,"Name":"Test",
    "subitem": [
        {"idenID":1,"Code":"254630"},
        {"idenID":2,"Code":"4566"},
        {"idenID":3,"Code":"4566"}
    ]
}];

console.log(JSON.parse(info)); //Uncaught SyntaxError: Unexpected token o 

Does anyone know how to properly de-serialize the 'info' variable into a JavaScript object?

Answer №1

The object is already of type Array. To access the Object:

var foo = data[0];

alert(foo.ID);

JSON.parse converts a String into an equivalent JavaScript value.

Answer №2

If you're working with JavaScript, knowing how to parse JSON is essential. This becomes particularly important when dealing with data in String format that has been fetched from the server side.

The main purpose of using JSON.parse is to convert JSON into a format that can be readily used as a JavaScript object. For instance,

let str = "{\"name\":\"John\",\"age\":30}";
let obj = JSON.parse(str); //obj = {name:'John', age: 30}

For more information, check out the MDN documentation.

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

The Value Entered in Angular is Unsaved

I have encountered an issue with my app's table functionality. The user can enter information into an input field and save it, but upon refreshing the page, the field appears empty as if no data was entered. Can someone please review my code snippet b ...

What is the best way to access automatically generated JavaScript variables in TypeScript?

I am currently facing an issue with integrating a Google login API in my React project and I need some help. The problem arises when the user already has an active session, rendering the API unnecessary. The Javascript solution provided is as follows: co ...

Tips for including an additional class in an element

I have an image tag with the "kiwi" class, and another class that makes it spin. I want to toggle this second class (called 'elem') when clicking on the play button of my music player. Here is the image tag with the class: <img src="./im ...

Establish a primary background color for the React application, with the majority of the display content

I have been working on styling a React project where App.js mainly handles routing and navigation to its components: App.js import {useState, useEffect} from 'react'; import Models from './pages/Models'; import Loadingpage from ' ...

Error: A DOMException was caught in the promise while trying to push the router

Currently, I am facing an issue while working with React. An Uncaught DOMException occurs when trying to push the router. This exception specifically happens when I attempt to push a new URL into the router using an event triggered from a button in a mod ...

Unintentional GET request triggered by Axios baseURL

I have encountered a strange issue where defining axios.defaults.baseURL = baseUrl; results in an unexpected GET request right after initializing my Vue app. Any assistance would be greatly appreciated! Below are images showing the code and network reques ...

It seems like the method has already been executed despite encountering an unexpected end of JSON input error

Currently, I am utilizing the etherscan API to retrieve logs for certain events. Although my JSON parsing method seems quite traditional, an error is being thrown stating "unexpected end of JSON". function getEventHistory() { const topic0 = web3.util ...

Loop through a JSON object within the collection of another ng-repeat loop

I'm facing an issue with my ng-repeat loop where I iterate through a JSON and display it in a table. The problem is that when I try to iterate through a nested JSON value, it's treating it as a string. <tr ng-repeat="(key,value) in event. ...

Displaying information and pictures in a list area from a JSON file with a feature similar to Lazy loading on Black

I have successfully created a list field that includes both a picture and some data in each row. The data and images are sourced from JSON. While my code is functioning, the UI becomes unresponsive once the list is populated. The process involves two steps ...

Is there a way to completely remove an element from the dom once the ajax call has returned

I've been struggling to completely remove LI elements and their content, including the checkbox input, from the DOM without success. After an ajax callback, I am calling the following function, but it only removes the contents and not the element its ...

Issue Installing Npm Package (detected 23 security vulnerabilities)

My attempt to install the package resulted in an error message. How can I resolve this issue? ...

Adjust CSS classes as user scrolls using skrollr

I am currently facing an issue with the prinzhorn/skrollr plugin when trying to use removeClass/addClass on scroll function. I have attempted to find a solution but unfortunately, nothing has worked for me. <li class="tab col s3"><a data-800="@cl ...

Glitch in Mean App: front-end feature malfunctioning during data storage in mongodb

I am encountering difficulties while working on a MEAN app. I attempted to establish a connection between the backend (Node.js, Express.js) and the frontend (Angular 6), but encountered some issues. The backend port is http://localhost:3000, and the fron ...

Creating a password with two distinct numbers using regular expressions

In javascript I am struggling to create a password that meets the criteria of having at least eight characters, including two SEPARATE digits, one uppercase and one lowercase letter, as well as one special character (-, @, #, $, &, *, +) but not /, !, ? ...

Providing UI field attributes within server JSON data

Suppose I have numerous form fields that need to be displayed on the user interface. Additionally, in a standard Modify workflow, let's assume that these fields will have various attributes like value, mandatory, editable, disabled, label, regex (for ...

javascript alter css property display to either visible or hidden

I am struggling with a CSS id that hides visibility and uses display: none. The issue arises when I want the element to be visible upon clicking a button, but removing the display:none property is causing design problems due to it being an invisible elemen ...

Troubleshooting problem with JavaScriptSerializer when parsing deserialized JSON data

Recently, I encountered a challenge while working on a program that involves extracting a specific value from a WebRequest response sent in JSON. I am using JavaScriptSerializer to parse the values but facing some difficulties. Here is a snippet of the cod ...

Updating an array using `setState` does not result in the array being updated

I created a component that uses the .map() method to render an array of students and has a button to shuffle and update the display. However, I'm facing an issue where the display does not update every time I click the button. const Home: NextPage = ...

Mysterious and never-ending loop that seems to loop endlessly and eludes my

My prototype includes a method for adding callbacks: /* * Add a callback function that is invoked on every element submitted and must return a data object. * May be used as well for transmitting static data. * * The callback function is supposed to e ...

During testing, the Vuetify expansion panel body is hidden with a display none style

Greetings! I am currently facing an issue while debugging my testing site. The problem is that the expansion panels are not displaying due to a style attribute attached to the div element of v-expansion-panel__body. Strangely, this issue does not occur on ...