Use Object.keys() to get keys array and use forEach() to iterate over them.

var data = {
  "VERSION": "2006-10-27.a",
  "JOBNAME": "EXEC_",
  "JOBHOST": "Test",
  "LSFQUEUE": "45",
  "LSFLIMIT": "2006-10-27",
  "NEWUSER": "3",
  "NEWGROUP": "2",
  "NEWMODUS": "640"
};

Object.keys(data).forEach(function(key) {
  console.log('Key : ' + key + ', Value : ' + data[key])
})

Answer from Pranav C Balan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-json-object-in-javascript
How to Iterate JSON Object in JavaScript? - GeeksforGeeks
July 23, 2025 - In JavaScript, there are various ways to iterate over a JSON object, such as using the for...in loop, Object.keys(), Object.entries(), and for...of. Each method provides a simple and effective way to access the keys and values of a JSON object, ...
Discussions

How do I loop through or enumerate a JavaScript object? - Stack Overflow
When using a for loop like that, key will just take on an index value, so that will just alert 0, 1, 2, etc... You need to access p[key]. 2009-03-26T06:07:13.447Z+00:00 ... It is the slowest method of array iteration in JavaScript. More on stackoverflow.com
🌐 stackoverflow.com
[HELP] How to loop through JSON keys?
Probably best to post your exported task description and and give a detailed description of where you are stuck. To post your profile or task here...  Long press on the profile or task name / ( 3 dot menu with 4.0+ ) export / export "DESCRIPTION" to clipboard (not XML) Any linked tasks will be exported with the profile they are linked to.. To be able to export, the profile needs to be named by you (Not the Tasker listed name.  Tasker will list your profile with the context name if you have not given it one). On older Tasker versions you might need to disable beginner mode in the Tasker preferences. Beginner mode has been removed from the latest Tasker release. More on reddit.com
🌐 r/tasker
8
3
March 27, 2021
javascript - iterating through object - Stack Overflow
I'm having a really hard time trying to find a way to iterate through this object in the way that I'd like. I'm using only Javascript here. First, here's the object { "dialog": { " More on stackoverflow.com
🌐 stackoverflow.com
Can I iterate through the key-value pairs of a JSON object with Serde(/anything-else)?
as_array or as_object More on reddit.com
🌐 r/learnrust
11
0
December 23, 2021
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Possible to iterate through a JSON object and return values of each property? - JavaScript - The freeCodeCamp Forum
July 3, 2017 - Hey all, I’m trying to add a shopping list to my Recipe box project as an extra personalization, and when researching on how to do this, one camper recommended that in order for me to render my Shopping list in a differ…
🌐
Futurestud.io
futurestud.io › tutorials › iterate-through-an-objects-keys-and-values-in-javascript-or-node-js
Iterate Through an Object’s Keys and Values in JavaScript or Node.js
March 3, 2022 - Object.keys(tutorials).forEach(key => { console.log(`${key}: ${tutorials[value]}`) }) // nodejs: 123 // android: 87 // java: 14 // json: 7 · The for…in loop exists in JavaScript for a long time. It was already supported in Internet Explorer 6. If you need to support old browsers, the for…in loop is a solid choice. This loop gives you access to each key in a given object. You can then access the related value using index access. Here’s an example iterating over an object and printing each key-value pair:
🌐
Coderwall
coderwall.com › p › _kakfa › javascript-iterate-through-object-keys-and-values
JavaScript iterate through object keys and values (Example)
June 26, 2023 - I just wanted to keep this for reference how to quickly loop through an objects keys and values, if needed. I also included an implementation using jQuery .each · Note the limitations of using a for...in loop, as it iterates over the properties of an object in an arbitrary order, and needs to use .hasOwnProperty, unless inherited properties want to be shown.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript - MDN Web Docs
Using array destructuring, you can iterate through objects easily. ... // Using for...of loop const obj = { a: 5, b: 7, c: 9 }; for (const [key, value] of Object.entries(obj)) { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" } // Using array methods Object.entries(obj).forEach(([key, ...
🌐
ZetCode
zetcode.com › javascript › jsonforeach
JavaScript JSON forEach - Iterating Over JSON Arrays
The fetch function retrieves data as JSON array from the provided URL. With forEach, we go through the array. Object.entries(obj).forEach(([key, value]) => { console.log(`${key} ${value}`); });
🌐
Medium
allaboutcode.medium.com › top-3-ways-to-loop-through-a-json-object-in-javascript-67ca21d33a24
Top 3 Ways to Loop Through a JSON Object in JavaScript | by Marika Lam | Medium
September 27, 2022 - These will return an array which we can then iterate over. Let’s take a look at using Object.entries. This returns an array of the key/value pairs of the object we pass it: const res = JSON.parse(xhr.responseText);Object.entries(res).forEach((entry) => { const [key, value] = entry; console.log(`${key}: ${value}`); });// id: SvzIBAQS0Dd // joke: What did the pirate say on his 80th birthday?
Find elsewhere
Top answer
1 of 16
5192

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

For-of with Object.keys() alternative:

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

Notice the use of for-of instead of for-in, if not used it will return undefined on named properties, and Object.keys() ensures the use of only the object's own properties without the whole prototype-chain properties

Using the new Object.entries() method:

Note: This method is not supported natively by Internet Explorer. You may consider using a Polyfill for older browsers.

const p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (const [key, value] of Object.entries(p)) {
  console.log(`${key}: ${value}`);
}
2 of 16
1383

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

You can combine for...of, destructuring, and Object.entries:

for (const [key, value] of Object.entries(obj)) {
    console.log(key, value);
}

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

🌐
Codemia
codemia.io › knowledge-hub › path › how_to_iterate_over_a_jsonobject
How to iterate over a JSONObject?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
Top answer
1 of 7
176

You use a for..in loop for this. Be sure to check if the object owns the properties or all inherited properties are shown as well. An example is like this:

var obj = {a: 1, b: 2};
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    var val = obj[key];
    console.log(val);
  }
}

Or if you need recursion to walk through all the properties:

var obj = {a: 1, b: 2, c: {a: 1, b: 2}};
function walk(obj) {
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      var val = obj[key];
      console.log(val);
      walk(val);
    }
  }
}
walk(obj);
2 of 7
14

My problem was actually a problem of bad planning with the JSON object rather than an actual logic issue. What I ended up doing was organize the object as follows, per a suggestion from user2736012.

{
"dialog":
{
    "trunks":[
    {
        "trunk_id" : "1",
        "message": "This is just a JSON Test"
    },
    {
        "trunk_id" : "2",
        "message": "This is a test of a bit longer text. Hopefully this will at the very least create 3 lines and trigger us to go on to another box. So we can test multi-box functionality, too."
    }
    ]
}
}

At that point, I was able to do a fairly simple for loop based on the total number of objects.

var totalMessages = Object.keys(messages.dialog.trunks).length;

    for ( var i = 0; i < totalMessages; i++)
    {
        console.log("ID: " + messages.dialog.trunks[i].trunk_id + " Message " + messages.dialog.trunks[i].message);
    }

My method for getting totalMessages is not supported in all browsers, though. For my project, it actually doesn't matter, but beware of that if you choose to use something similar to this.

🌐
ServiceNow Community
servicenow.com › community › developer-forum › parse-json-array-to-get-key-value-pair › m-p › 2470335
Solved: Re: parse json array to get key value pair - ServiceNow Community
February 7, 2023 - Using the following code, you can iterate through the JSON array and print the key-value pairs: var gr = new GlideRecord('incident'); gr.addQuery('number', 'INC0001164'); gr.query(); if (gr.next()) { var mrvs = gr.variables.employee_details; var Payload = JSON.parse(mrvs); for (var i = 0; i < Payload.length; i++) { var obj = Payload[i]; var keys = Object.keys(obj); for (var j = 0; j < keys.length; j++) { var key = keys[j]; if (key == "name") { gs.print("Name: " + obj[key]); } if (key == "manager") { gs.print("Manager: " + obj[key]); } } } } 1 Helpful ·
🌐
SitePoint
sitepoint.com › blog › javascript › how to loop through a json response in javascript
How to Loop Through a JSON Response in JavaScript — SitePoint
February 15, 2024 - It is language-independent, but inspired by JavaScript object literal syntax, with the key difference that in JSON, keys must be quoted with double quotes. Parsing JSON data from a server involves two steps: first, decoding the JSON string into a native JavaScript data structure (such as an array or object), and then iterating over this structure using JavaScript’s built-in methods like for...in, Object.entries, Object.values, or array methods such as forEach.
🌐
W3Schools
w3schools.com › js › js_json_objects.asp
JSON Object Literals
Each key/value pair is separated by a comma. It is a common mistake to call a JSON object literal "a JSON object". JSON cannot be an object. JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
🌐
Microverse
microverse.org › home › blog › how to loop through the array of json objects in javascript
How to Loop Through the Array of JSON Objects in JavaScript
September 29, 2022 - Here’s an example; you’ve got an object containing some properties and you need to look up each property and the value that it carries. Here’s how you would use the For In Loop to do so: {% code-block language="js" %} var person = { fname: "Nick", lname: "Jonas", age: 26 }; for (let x in person) { console.log(x + ": "+ person[x]) } ‍{% code-block-end %} JSON stands for JavaScript Object Notation.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript foreach json key-value | example code
JavaScript foreach JSON key value | Example code
January 27, 2023 - Simple example code used Object.keys() to get the keys array and use forEach() to iterate over them. <!DOCTYPE html> <html> <body> <script> var data = { "VERSION": "1001.10", "JOBNAME": "EXEC_", "JOBHOST": "Loal", "LSFQUEUE": "80", "LSFLIMIT": ...