I hope.. It should do the trick

var inputString=[{"rKVAH_Lag":"xyz","Asset_Name":"GSM Meter"}];

var keys=[]
var values=[]
i=0
for (var key in inputString[0]) {
    keys[i]=key;
    values[i]=inputString[0][key]    
    i=i+1
}
console.log(keys);
console.log(values);
Answer from Mahesh on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-a-value-from-a-json-array-in-javascript
How to Get a Value from a JSON Array in JavaScript? - GeeksforGeeks
June 28, 2025 - To retrieve a value from a JSON array in JavaScript, we can use various methods such as accessing the array by index or using built-in methods like find(), map(), etc. In a JSON array, values are stored in an ordered list, which means you can ...
Discussions

how to get value of json array object in javascript or jquery - Stack Overflow
in this code response is coming in from of above image. i want to store key and value in arr. ... It seems to be a string with an object definition actually. Check out JSON.parse() ... sir, I am getting only [object object] but i want to store all key in one array and value in another array. More on stackoverflow.com
🌐 stackoverflow.com
javascript - extract array from json object - Stack Overflow
How can we retrieve from this object array of only X2 key value pairs. ... The key does not have to contains number. What I need here is a function that will recieve parameter - i and return an array of all the i-th objects in the original json object More on stackoverflow.com
🌐 stackoverflow.com
javascript - get values in pairs from json array - Stack Overflow
I have tried many answers for this, like: Javascript: Getting all existing keys in a JSON array and loop and get key/value pair for JSON array using jQuery but they don't solve my problem. More on stackoverflow.com
🌐 stackoverflow.com
How to get values from json array in a javascript - Stack Overflow
I'm trying to get values from json array inside a javascript. Below is the part of my javascript file. I'm using jsondata.value to get the values. It's displaying the json data correctly on the c... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
🌐
Delft Stack
delftstack.com › home › howto › javascript › get value from jason object in javascript
How to Get Value From JSON Object in JavaScript | Delft Stack
February 2, 2024 - const json = `{ "employee": { "name": "Roy Mustang", "age": 35, "favoriteColor": ["Blue", "Black", "Purple"], "siblings": { "Liza": 20, "Emily": 12 } } }`; var data = JSON.parse(json); var i; for (i in data) { if (data[i] instanceof Object) { console.log(data[i]); } } ... As we have already parsed the data, now let’s extract the individual values for key:value pair with the dot (.) operation.
🌐
TutorialsPoint
tutorialspoint.com › javascript-how-to-get-all-name-values-in-a-json-array
JavaScript How to get all 'name' values in a JSON array?
Let’s say the following is our JSON array − · To get only the CustomerName values, use the concept of map() −
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › parse
JSON.parse() - JavaScript - MDN Web Docs
5 days ago - The Object, Array, string, number, boolean, or null value corresponding to the given JSON text. ... Thrown if the string to parse is not valid JSON. JSON.parse() parses a JSON string according to the JSON grammar, then evaluates the string as if it's a JavaScript expression.
🌐
Stack Overflow
stackoverflow.com › questions › 39582566 › how-to-get-values-from-json-array-in-a-javascript
How to get values from json array in a javascript - Stack Overflow
May 22, 2017 - var gridOptions = { ... [your options here] }; document.addEventListener('DOMContentLoaded', function() { var gridDiv = document.querySelector('#myGrid'); new agGrid.Grid(gridDiv, gridOptions); var jsondata = document.getElementById("jsonArray").value; var jsondataObj = JSON.parse(jsondata); function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } var parsedData = jsondataObj.map(function(obj) { return Object.keys(obj).reduce(function(memo, key) { var value = obj[key]; memo[key] = isNumeric(value) ?
🌐
I'd Rather Be Writing
idratherbewriting.com › learnapidoc › docapis_access_json_values.html
Access and print a specific JSON value | I'd Rather Be Writing Blog and API doc course
2 days ago - JSON wouldn’t be very useful if you always had to print out the entire response. Instead, you select the exact property you want and pull that out through dot notation. The dot (.) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you ...
🌐
W3Schools
w3schools.com › js › js_json_objects.asp
JavaScript JSON Values
Array values can be any valid JSON value. Objects and arrays can contain other objects and arrays.
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-get-the-values-from-json-of-array › m-p › 2555131
Solved: Re: How to get the values from json of array - ServiceNow Community
May 8, 2023 - var parsed = JSON.parse(JSON.stringify(obj)); // No need for JSONParser var length = parsed.items.length; gs.log(length); for (var i = 0; i < length; i++) { var item = parsed.items[i]; gs.log("Item ====" + item.displayName); var systemDevices = item.systemDevices; var systemDevicesLength = ...
🌐
YouTube
youtube.com › watch
How to get JSON data Array value access || JSON Basics - YouTube
JSON Course covers everything from start to finish to get you using JSON quickly!•Learn the basics of JSON •JSON structure data of delivery•basics of JavaScr...
Published: March 25, 2016
🌐
Reddit
reddit.com › r/learnjavascript › cannot access fields from a json array object with javascript
r/learnjavascript on Reddit: Cannot access fields from a json array object with javascript
July 1, 2022 -

Hi!

I have a fragment of code that executes an ajax request and returns me this json (formatted a little to look decent on reddit):

[{"id":698,"song_name":"never too late","song_artist":"Three Days Grace","song_album":"One-X","song_duration":"00:03:29"},
{"id":501,"song_name":"new romantics","song_artist":"Taylor Swift","song_album":"1989","song_duration":"00:03:57"},
{"id":686,"song_name":"no reason","song_artist":"Sum 41","song_album":"Chuck","song_duration":"00:03:04"},
{"id":909,"song_name":"now my feet won't touch the ground","song_artist":"Coldplay","song_album":"Prospekt's March EP","song_duration":"00:02:30"},
{"id":517,"song_name":"numb","song_artist":"Linkin Park","song_album":"Meteora","song_duration":"00:03:49"}]

I've been trying to access the fields of this json object for a while now but I cannot figure out for the life of me what i'm doing wrong. I am very new to javascript so it's probably something very silly.

my code:

function querySongs() {
            let song = 'n'
            let xhr = new XMLHttpRequest()

            xhr.open('GET', 'http://127.0.0.1:8000/music_tracker/get_song_by_name/?song_name=' + song, true)
            xhr.setRequestHeader("Content-Type", 'application/json;')

            xhr.onload = function() {
                if (xhr.status == 200) {
                    let songs = JSON.parse(this.response)
                    console.log(songs)  // this prints array indexes

                    for(var song in songs){  
                        console.log(song.song_name) // this one gives undefined
                    }
                }
            }

            xhr.send()

        }

Thank you in advance for your help! :)

Top answer
1 of 3
3
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
2 of 3
2
I agree with u/ProposalUnhappy9890 . Fetch is the preferred method of getting JSON data. The MDN docs basically say as much. Here is an example: 1 const data = './data.json 2 3 fetch(data) 4 .then(response => { 5 return response.json(); 6 }) 7 .then(data => console.log(data)); I'll break this down. Line 1: you are declaring a variable that points to where your data is stored. In this case, it is a local file. It could also be a URL for an API endpoint. Line 3: We execute the fetch() method and pass in the location of our JSON data using the variable we just declared. You could just as easily put the location in the parameters. The fetch() method will return a promise. Line 4: .then() waits for the promise to be returned. It is common to use an arrow function to handle the promise. We give the promise a variable name "response". Line 5: We do something with the "response" variable and return it. Here we are using the .json() method ( more on that ). This will return a JSON response into something that will make sense to JS. That could be an object, an array, or some other data type. Line 7: Again, we are going to handle the data that was returned by lines 4 & 5. We assign a new variable named "data" and use an arrow function to do something with it. Here we are just logging the response to the console, so you should see it in your browser console when this is executed. To make this more clear, if you replace return response.json() in line 5 with return 'Some Text', then your console output would no longer be your JSON array, it would be the words 'Some Text'. Let's go one step further to drive the point home. In line 7, if you replaced console.log(data) with console.log(data.toUpperCase()), then your console would now log "SOME TEXT".
🌐
W3Schools
w3schools.com › js › js_json_parse.asp
JavaScript JSON.parse()
After parsing, you can use normal JavaScript array methods. JSON.parse() can parse any valid JSON value. Convert any JSON text into a JavaScript value.
🌐
TutorialsPoint
tutorialspoint.com › how-to-read-data-from-json-array-using-javascript
How to read data from JSON array using JavaScript?
July 18, 2020 - Following is the code to read data from JSON array using JavaScript − Example Live Demo
🌐
Squash
squash.io › how-to-convert-json-object-to-javascript-array
How to Convert JSON Object to JavaScript Array - Squash Labs
In this example, we first use Object.values() to get the property values of the jsonObject, and then we use Array.from() to convert the property values to an array. Related Article: How To Remove Duplicates From A Javascript Array
🌐
Tutorial Republic
tutorialrepublic.com › javascript-tutorial › javascript-json-parsing.php
JavaScript JSON Parsing - Tutorial Republic
In JavaScript, you can easily parse JSON data received from the web server using the JSON.parse() method. This method parses a JSON string and constructs the JavaScript value or object described by the string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-json-arrays
JavaScript | JSON Arrays | GeeksforGeeks
May 28, 2021 - The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi ...
🌐
Thequantizer
thequantizer.com › the quantizer › javascript
JavaScript: How to Get JSON value by Key :: The Quantizer
let firstKey = Object.keys(fir... a few ways to do this. One of the most common is a forEach loop. jsonData.forEach((element, i) => { console.log("element: " + i + " is: " + JSON.stringify(element)) });...