Ways to retrieve Json Data

I am facing an issue while trying to extract data from a JSON array with nested arrays that contain spaces in their key names. Each time I attempt to execute the code, it results in an error.

var sampleError = [
    {
        "LessonName": "Understanding Multiplication",
        "LessonID": "13343",
        "no of questions": [{"Locked": "31", "Unlocked": 5}]
    },
    {
        "LessonName": "Finding Unknown Values",
        "LessonID": "13424",
        "no of questions": [{"Locked": "34", "Unlocked": 5}]
    }
]

function jsd(){
    document.write(sampleError[0].LessonName);
    document.write(sampleError[0]['no of questions'][0].Locked);
}

document.write(sampleError[0]["no of questions"][0].Locked);
does not appear as expected.

Answer №1

For accessing the property, you can utilize a technique known as a property accessor using brackets in the string.

error[0]['no of questions'][0].Locked

Answer №2

Remember to utilize this specific syntax when dealing with strings that contain spaces.

document.write(error[0]['no of questions'][0].Locked);

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

Making an Ajax request to retrieve progress information by utilizing IProgress

I have encountered an issue with my code involving 2 ajax API calls. One call fetches data through a lengthy process, while the other retrieves progress values using the IProgress interface and runs every 5 seconds. The value from ReportProgress successf ...

Is it possible to execute node.js code within a Java script environment?

I have a javascript code that functions as a discord bot. Typically, to run this bot, I would open the command prompt in the js path and enter node {file_name}.js. Now, let's say I have another javascript code that I want to execute on button click an ...

What is the best way to disable the functions doajax_red and doajax_blue when a radio button is checked?

Is there a way to halt the functions doajax_red and doajax_blue when a radio button is checked? Upon loading the page index.php, clicking the red button will display a loading image. If you check the BLUE radio button and then re-check the RED radio butt ...

Use jQuery to permit only numeric characters and the symbol "-" to be entered into a textbox

I have been working on a JQuery script that only allows users to input numbers in a text field. However, I am now trying to modify the code to also allow users to enter "-" (hyphen), but so far it's not working as expected. If anyone can provide assis ...

Is there a way to "stream" a specific part of my website to a separate window?

Is there a way to stream a specific container from my webpage to another window? The scenario is similar to a Point of Sale system, where there is an operator-facing display and a second customer-facing display. The operator-facing display will show all ...

What could be causing the NoScript tag to malfunction across different web browsers?

I've incorporated the NoScript tag into my JSP pages in the head section. To avoid conflicts with tiles, I made sure not to include multiple NoScript tags. However, I am experiencing issues in Internet Explorer where it doesn't seem to be working ...

Utilize Angular functions in every controller in your application

Currently, I have important functionality code within app.run(function... in Angular: app.run(function($rootScope, $window) { // Obtain window dimensions $window.width = angular.element($window).width(); $window.height = angular.element($window).he ...

Tracking the number of images and adjusting the counter as the user scrolls

I am working on a project where I have a series of images displayed in a column. I would like to add a dynamic counter to indicate which image is currently in focus based on the scroll position. Feel free to check out the demo <div class="counter"> ...

Replacing the string PHP in a TEXT field in a MySQL database

My database contains JSON objects that store images I want to display. However, I am unsure of how to perform a string replace operation to remove the brackets, quotes, and spaces in order to retrieve just the random string.png. ["4a21da2670ce6528b2cffebf ...

Issue with React Material UI Menu not closing properly

I am struggling to close this menu once it is opened on the page, despite trying various methods like moving the icon button tags around. const useStyles = makeStyles((theme) => ({ root: { flexGrow: 1, }, menuButton: { marginRight: theme.s ...

Determine the presence of a value within a specific MySQL column

Is it possible to verify the existence of a value in a MySQL column? Within my 'songs' table, there is a column named 'agent_ip' where I store a list/array of all user IPs that visit the site. I am trying to determine if the user's ...

Issue with saving values in GridView using Kartik/EditableColumn in Yii2

I am experiencing an issue with the EditableColumn in Yii2. It is not saving the values of the changes I make. In my GridView index, I have the following setup: [ 'class'=>'kartik\grid\EditableColumn', 'attribut ...

Retrieval is effective in specific situations but ineffective in others

I have encountered an issue with fetching data only when using the async behavior. I am currently in the process of re-building a property booking website that was originally developed using Laravel and a self-built API. The new version is being created wi ...

Turn off the ability to view the content of .css and .js files within the browser

Earlier, I inquired about how to disable file and folder listing and discovered that it can be achieved using a file named .htaccess. To disable folder listing, I entered Options -Indexes in the .htaccess file located in the parent folder. Additionally, to ...

IE9 does not consider JavaScript variable as undefined

Hello, I came across this JS code written by someone else: function somename(f) var procName = ""; var procParams = new Array(); var parseEl = ""; var parseEls = new Array(); var parseInd = 0; var procParamsInd = 0; var IsVal ...

I am trying to figure out the best way to position the navbar directly under the jumbotron in Bootstrap 4

Obtaining information is possible with Bootstrap 3, yet I am struggling to understand how to implement it with Bootstrap 4. ...

What is the process for generating a list of dictionaries where each dictionary is derived from the same original dictionary but with one key excluded?

My current situation: I am faced with the task of iterating through a nested JSON object, extracting and returning a list of JSON objects, each with one key removed. The challenge is that I do not have prior knowledge of the data structure. What I have tr ...

What is the best way to add a border around an image along with a button using VueJS?

I am struggling to link a button and an image in VueJS to display a border around the picture. While I can successfully display the border on the button, I am unsure how to extend it to the image as well. Vue.component('my-button', 'my-img& ...

What is the most effective method for destructuring within React components?

Observing how people implement destructuring in functional components in React, I have noticed a common pattern. const InputGroup = ({ name, placeholder, value }) => ( However, my preferred method differs: const InputGroup = props => { ...

Angular select element is not functioning properly with the `addEventListener` method

My current project involves creating a table using the primeng library. The table consists of three rows and three columns, and all the data is static. Even though I am utilizing an external library, I find myself traversing the DOM directly. <p-table ...