Javascript has a built in JSON parse for strings, which I think is what you have:
var myObject = JSON.parse("my json string");
to use this with your example would be:
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Here is a working example
EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage
IMPORTANT: the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).
Javascript has a built in JSON parse for strings, which I think is what you have:
var myObject = JSON.parse("my json string");
to use this with your example would be:
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Here is a working example
EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage
IMPORTANT: the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).
In a for-in-loop the running variable holds the property name, not the property value.
for (var counter in jsonData.counters) {
console.log(jsonData.counters[counter].counter_name);
}
But as counters is an Array, you have to use a normal for-loop:
for (var i=0; i<jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Parsing JSON Request. Find object in array
Parse JSON into array
Large array of objects in JSON for JSON.parse() in JavaScript
The issue is that JSON.parse takes a string; you're passing it an object literal. It's used for taking a string that contains JSON-formatted data and converting it into a JS object. But what you have here doesn't need to be parsed; you can just set a variable equal to it directly.
Alternatively, wrap the entire contents inside JSON.parse() with single-quotes or back-ticks (the ` character).
creating a dictionary with the first value as a key, and the 2nd and 3rd fields concatenated somehow as the value.
Don't concatenate them; use objects.
More on reddit.comwhy is JSON.parse way slower than parsing a javascript object in the source itself?
Videos
Hi
I'm trying to parse a JSON response to a GET request and store the values of an attribute into an array. The JSON input looks like this:
[ { "created": "2021-01-23 22:30:00", "title": "title1"}, { "created": "2021-01-23 22:30:00", "title": "title2"}, ... ]
And I'd like the array to look something like ["title1", "title2", ...]. I've tried the following JS snippet, but I get undefined:
var data = JSON.parse(local('http_data'));
setLocal("titles", data.map(el => el.title));
%titles give 'undefined'.
Anyone know how to go about this?