What is the best way to access a specific value within a two-layered JSON object using JavaScript?

Here is an example of JSON data that I am working with:

{"0":{"access_token":"ya29.MgCIagT8PCpkRSIAAAAl-XYEA37OjX_GBAv4so6qv0Gowc5XD3Bp6MuwYAPmnNuwgz7ElXsRwXqGWL4aZpA","token_type":"Bearer","expires_in":"3600","scope":"https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.moments.write https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.agerange.read https://www.googleapis.com/auth/plus.profile.language.read https://www.googleapis.com/auth/plus.circles.members.read","id_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6IjRiMTEyMDUxY2M3ZmJmZWI0MmI3OWU0OWVkNDIwYTQ2Yjk1YTkwYTQifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTE1NTIzMTUwNzczOTI1MTk0ODE4IiwiYXpwIjoiMTUwNDU0MzcwMTUxLWM4aGFubnV0NXE0cWhrNzM3bTB0MGVpNjBnM29zcjlhLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXRfaGFzaCI6IkszS1UyNjRBY3QxeWh...

However, when I try to retrieve the value using obj.access_token, it returns "undefined".

Answer №1

Give this a shot:

var data = {"0":{"auth_token":"ya29.MgCIagT8PCpkRSIAAAAl-XYEA37OjX_GBAv4so6qv0Gowc5XD3Bp6MuwYAPmnNuwgz7ElXsRwXqGWL4aZpA","token_type":"Bearer","expires_in":"3600","scope":"https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.moments.write https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.agerange.read https://www.googleapis.com/auth/plus.profile.language.read https://www.googleapis.com/auth/plus.circles.members.read","id_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6IjRiMTEyMDUxY2M3ZmJmZWI0MmI3OWU0OWVkNDIwYTQ2Yjk1YTkwYTQifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTE1NTIzMTUwNzczOTI1MTk0ODE4IiwiYXpwIjoiMTUwNDU0MzcwMTUxLWM4aGFubnV0NXE0cWhrNzM3bTB0MGVpNjBnM29zcjlhLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXRfaGFzaCI6IkszS1UyNjRBY3QxeWhfbXZ6TlI4UXciLCJhdWQiOiIxNTA0NTQzNzAxNTEtYzhoYW5udXQ1cTRxaGs3MzdtMHQwZWk2MGczb3NyOWEuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJjX2hhc2giOiIxOE9qaWtOM1NOWnh6VVNCQWkxZnJBIiwiaWF0IjoxNDAzOTg1ODQzLCJleHAiOjE0MDM5ODk3NDN9.YWqarKgkXIi1u8v3jZ5726GHeTUn14Vvh3NryaDVg6M-e8a4a74HqhVt6zixjq35Minc_cCMxSOKIotBZDp1aG0_qKypRnatOJr3SM1-wIz779FNMUbS31y53S9NRHxOiE41Dtwvyyc6Np69K1RKT8f4FHM96XzAnLiJolAmT00","num_sessions":"1","session_state":"04e6cecd3bb8aa7993e6867475eaa3c9f50f9e19..48fa","client_id":"150454370151-c8hannut5q4qhk737m0t0ei60g3osr9a.apps.googleusercontent.com","response_type":"code token id_token gsession","issued_at":"1403986143","expires_at":"1403989743","_aa":"0"}};
alert(data["0"].auth_token);

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

Configuring Dialog Placement to the Top in Material-UI

I'm in the process of creating a popup dialog, but I'm encountering an issue where the dialog pops up in the center of the page. How can I position it at the very top of the page when the popup button is clicked? Below is the code snippet: < ...

Can a React component be configured to show only a specific array key when returning an array?

Just diving into the world of react and experimenting with different approaches to understand how I can transition into this new architecture. Currently, I have a basic component where I am returning an array of elements (mainly for testing purposes): cl ...

I am in possession of a JSON file that lacks keys, and my objective is to extract the values and store them in a C# class object

Here is a JSON sample that I need to parse: [ [ 1617235200000, "58739.46000000", "59490.00000000", "57935.45000000", "58720.44000000", "47415.61722000", 1617321599999, &quo ...

The longevity of PHP variables when making Ajax requests

In most cases, variables are destroyed and their memory is freed by the garbage collector once it deems it necessary. This usually happens shortly after the script has finished executing. My curiosity lies in how long Ajax retrieved variables persist. If ...

Toggle the Material UI checkbox based on the value received from an object input

I am facing an issue with an unchecked checkbox in my project. I am attempting to update its value based on data retrieved from an object. The object contains boolean values from an SQL query, either 'T' for true or 'F' for false. My in ...

Tips for establishing a connection with the MovieDB API web service

As a beginner in the world of web services, I am currently in the learning phase. Unfortunately, there is not much online content available that provides information on how to use the MovieDB API web service. For now, my main focus is on trying to display ...

Hover over parts of an image to bring attention to them

I am interested in developing a webpage featuring a black and white image of 5 individuals. When hovering over each person, I would like them to illuminate and display relevant information in a dialog box next to them. Do you have any tips on how I can ac ...

Breaking down a string and then retrieving elements from an array

Just diving into the world of Javascript and jQuery, so I have a simple query. I've got a date that I need to break down into a string and then display it as an array. var date = "12/10/2010"; var dateArray = date.split(/); $('#printbox') ...

The server encountered a "Cannot GET /socket.io" error while trying

I am experiencing an issue with my app that uses express/socket.io. The app is running smoothly without any errors, but when I make an http-request, I receive the following error message: GET http://xxxxxxx.com:3035/socket.io/1/?t=1449090610579 400 (Bad R ...

The alert function is not being triggered upon receiving a JSON response

I am having trouble with an alert not firing a json response. The response appears in firebug, but after upgrading from php4.4.7 to php5.3.5, I encountered this error. It could be my mistake as well. Could someone please review my code and point out where ...

What is the best way to format a time in a 12-hour clock using JavaScript?

Is it possible to create a timer that performs an action after 12 hours? I want the timer to start at 6am and end at 6pm, lasting for exactly 12 hours. Additionally, I'm interested in converting my current 12-hour clock into a 24-hour format. I am se ...

Experiencing a missing handlebars helper error when utilizing a helper within partials in express-handlebars

I have set up custom helpers using express-handlebars like this: const { create } = require("express-handlebars"); // Configuring the handlebars engine const hbs = create({ helpers: require("./config/handlebars-helpers"), }); app.engi ...

Surprising outcomes encountered when playing audio with JavaScript

https://i.sstatic.net/1jz45.png I've been diving into learning JavaScript and decided to create a simple web page. This page, when Pikachu (image) is clicked, plays an audio file. Similarly, if the string "Pikachu" is typed into the form, it should ...

Receiving error message "[object Object]" while working with JavaScript

My current challenge involves adding an item to the shopping cart through a button click event that sends necessary data to a database table storing cart items. The issue arises with the item name, as I am encountering an error displaying [object Object] ...

Utilize ZLIB and Node.js to create a compressed zip archive of a folder's contents

I need to compress all the files in a directory into a single ZIP file. Currently, I am using this code snippet: var fs = require('fs'); var tar = require('tar'); var zlib = require('zlib'); var path = require('path&apo ...

I am unable to enter any text in an angular modal

Currently, I am facing an issue where I am unable to click on the search input field within a modal. The goal is to implement a search functionality in a table displayed inside a modal window. The idea is to have a list of hospitals where users can view d ...

React with TypeScript presents an unusual "Unexpected token parsing error"

Recently, I encountered an issue with my helper function that was originally written in JavaScript and was functioning perfectly. However, as soon as I introduced TypeScript into the mix, strange behaviors started to surface. Here is the snippet of code fr ...

Displaying information in form using ajax within the laravel framework

I am currently utilizing ajax to retrieve data from a database. While I am able to successfully retrieve the data on the backend, I am facing difficulties displaying it in the input field below. I have tried writing some code, but it seems that the JavaScr ...

Maximizing the potential of NPM modules by harnessing the power of the package.json file

Currently, I am working on developing an NPM module for a command-line tool. Upon installation of the package, it is essential to access and read the user's package.json file. While I understand how to read the file syntactically, my main concern lies ...

React is struggling to locate the specified module

Once I've set up my react project using npx create-react-app called my-app, I proceed to run npm start only to encounter the error shown in the image below. Running node version: 12.16.1 with npm version: 6.13.4 View the error message her ...