I have an API endpoint that returns some data in the below format.
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
I thought it would be relatively trivial to be able to iterate through each key-value pair in this JSON payload, but I seem to be having errors with it. The code I have below is what I am using to first parse the JSON from string format into a variable, then I want to iterate through each key in the JSON object, retrieve the key and value, and then do something with both of those values.
var dictionary = JSON.parse(data);
for (var key in dictionary) {
var identifier = key;
var identifierValue = dictionary[key];
//do stuff..
I'm not sure whether the for() is valid in this instance, how exactly can I iterate through each key-value pair in the JSON object?
My issue is that I can't seem to access they keys or values held in the dictionary variable. It seems as though the for loop isn't working, which makes me feel like it's invalid for this use case. If this was C# I'd have to do something similar to
for(var key in dictionary.keySet)
{
//do...
}
I'm looking for the equivalent in JS.