var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
    
for (var i = 0; i < arr.length; i++){
  document.write("<br><br>array index: " + i);
  var obj = arr[i];
  for (var key in obj){
    var value = obj[key];
    document.write("<br> - " + key + ": " + value);
  }
}

note: the for-in method is cool for simple objects. Not very smart to use with DOM object.

Answer from Your Friend Ken on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to get "json.forEach" to work in Code Pen - JavaScript - The freeCodeCamp Forum
September 23, 2021 - I’m doing the first Data Visualization project, and I’m just trying to get basic things to function before tackling the main problem. To start I’ve copied over the of the exercises into the Code Pen below (with a new API), but whenever I click the button I just get “Uncaught TypeError: json.forEach is not a function.” I’ve already added d3 libraries, and a bunch of json ones, what am I missing to get “forEach” to work?
Discussions

How to loop through each item of a json array?
I have a .json file that I am using to have data for each level. I have an array in the .json file that i need to loop through the data for a level to generate it, except when i use this: for(i in leveldata){ loadLevel(levelData[i]); } it gives an error saying You can't iterate on a Dynamic ... More on community.haxe.org
🌐 community.haxe.org
0
0
April 27, 2021
How to loop through an array with JSON objects
const arr = [ {title: " some title", id: "some id"}, {title: " some title2", id: "some id2"} ] console.log( "For loop") for( let i = 0; i< arr.length; i++) { console.log(arr[i].id) } console.log( "Use of") for ( let item of arr) { console.log( item.id) } console.log( "Use forEach - recommended") arr.forEach( (item) => console.log( item.id)); console.log( "Use forEach plus destructuring - recommended") arr.forEach( ({id}) => console.log( id)); console.log( "Use map to transform array, then log array") const result = arr.map( ({id}) => id) ; console.log( result) Output: For loop some id some id2 Use of some id some id2 Use forEach - recommended some id some id2 Use forEach plus destructuring - recommended some id some id2 Use map to transform array, then log array [ 'some id', 'some id2' ] P.S. If you are using var to declare a variable, you probably shouldn't be. Use const and let where possible. More on reddit.com
🌐 r/learnjavascript
9
4
December 30, 2022
Use of .Object in this forEach loop example
json.forEach(function(val) { var keys = Object.keys(val); html += " "; keys.forEach(function(key) { html += "" + key + " " + val[key] + " ;"; }); html += " "; }); This is from challenge https://learn.freecodecamp.org/data-visualization/json-apis-and-ajax/convert-json-data-to-html The part that’s ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
January 24, 2019
Iterate through json with javascript (forEach)
Platform information: Hardware: Raspberry Pi 4 with 4GB OS: Openhabian Java Runtime Environment:OpenJDK Runtime Environment Zulu11. openHAB version: 3.1.0.M5 I have a json-object which I like to evaluate with javascript. I wrote a script (or copied it from different places of the internet xD) ... More on community.openhab.org
🌐 community.openhab.org
1
0
June 26, 2021
Top answer
1 of 3
1

The names are a little confusing because of O and 0 ! But you are almost there, here is the fixed code which at least parses your sample json data correctly:

public class MyModel
{
    public Outputdata Outputdata { get; set; }
}

public class Outputdata
{
    public TASK_0 TASK_0 { get; set; }
    public string ERROR_O { get; set; }
}

public class TASK_0
{
    public List TASK_O_ITEM { get; set; }
}

public class TASK_O_ITEM
{
    public string CODE { get; set; }
    public string TASK_NAME { get; set; }
}

And the usage:

var responseBody = "the body from wherever you read";
var result = JsonConvert.DeserializeObject(responseBody);
foreach (var item in result?.Outputdata?.TASK_0?.TASK_O_ITEM)
    Console.WriteLine($"{item.CODE} - {item.TASK_NAME}");
2 of 3
0

Hi @Paul

My thoughts are:

  1. Declare classes and lists.
  2. Assign a value to the data.
  3. Generate JSON variables.
  4. Print it out

For demonstration purposes, I printed the JSON data as string data.

In order to keep the same format as JSON, I set an option.

var options = new JsonSerializerOptions { WriteIndented = true };

string jsonString = System.Text.Json.JsonSerializer.Serialize(outputdata, options);

Of course, you can also print it directly.

You can refer to the following code:

public class TASK_O
{
    public List TASK_O_ITEM { get; set; }
}
public class Outputdata
{
    public TASK_O TASK_O { get; set; }
    public string ERROR_O { get; set; }
}
public class Example
{
    public Outputdata Outputdata { get; set; }
}
public class TASK_O_ITEM
{
    public string CODE { get; set; }
    public string NAME { get; set; }
}
internal class Program
{
    static void Main(string[] args)
    {
        var task_o_item = new List {
            new TASK_O_ITEM
            {
                CODE = "123",
                NAME = "ABC"
            },
            new TASK_O_ITEM
            {
                CODE = "456",
                NAME = "DEF"
            },
            new TASK_O_ITEM
            {
                CODE = "789",
                NAME = "GHI"
            },
        };
        var task_O = new TASK_O
        {
            TASK_O_ITEM = task_o_item
        };
        var outputdata = new Outputdata
        {
            TASK_O = task_O,
            ERROR_O = null
        };
        var example = new Example
        {
           Outputdata=outputdata
        };
        var options = new JsonSerializerOptions { WriteIndented = true };
        string jsonString = JsonSerializer.Serialize(example, options);
        Console.WriteLine(jsonString);
        Console.Read();
    }
}

Result:

Best Regards

Qi You


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

🌐
Haxe Community
community.haxe.org › t › how-to-loop-through-each-item-of-a-json-array › 2992
How to loop through each item of a json array? - haxe-js - Haxe Community
April 27, 2021 - I have an array in the .json file that i need to loop through the data for a level to generate it, except when i use this: for(i in leveldata){ loadLevel(levelData[i]); } it gives an error saying You can't iterate on a Dynamic ...
🌐
Reddit
reddit.com › r/learnjavascript › how to loop through an array with json objects
r/learnjavascript on Reddit: How to loop through an array with JSON objects
December 30, 2022 -

Hi all,

Im really struggling with this problem today. So basically, I have an array in the format

arr = [{title: " some title", id: "some id"}, {title: " some title2", id: "some id2"}] and all im trying to do is loop through each item in the array and get the value of the ids.

Here is what ive tried:

for( var i = 0; i< arr.length; i++){

console.log(arr[i].id)

}

It keeps showing up as undefined, please can anyone assist me? I would like the result to be "some id"

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Use of .Object in this forEach loop example - JavaScript - The freeCodeCamp Forum
January 24, 2019 - json.forEach(function(val) { var keys = Object.keys(val); html += "<div class = 'cat'>"; keys.forEach(function(key) { html += "<strong>" + key + "</strong> " + val[key] + "</br>;"; }); html += "</div><br>";…
Find elsewhere
🌐
HackMD
hackmd.io › @lnk › r1oVE9t2_
JS 把Json物件forEach出來 - HackMD
# JS 把Json物件forEach出來 ---- ###### tags: `javascript` `json` 使用Javascript時 很常遇到Json丟資料 **Json資料裡不能有空格跟換行符號!!!不然資料會抱錯** 當前端Javascript接到JsonString值時 可以轉成Json物件來使用 ```javascript= var s = '{"num":"4","ArticleTitle":"sss","AuthorName":"sss","ArticleUrl":"","SetTime":"2021-06-30 14:06:34.810","StartTime":null,"EndTime":null,"Publish":"1","ArticleContent":"sss","status":"check"}'; var row = JSON.parse(s); console.log(row); ``` ![](https://i.imgur.com/HMfDn7i.png) ![](https://i.imgur.com/nIL8LGo.png) 但
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript | MDN
// 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, value]) => { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" });
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › map
Array.prototype.map() - JavaScript | MDN
Since map builds a new array, calling it without using the returned array is an anti-pattern; use forEach or for...of instead.
🌐
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
Object.entries(tutorials).forEach(([key, value]) => { console.log(`${key}: ${value}`) }) // nodejs: 123 // android: 87 // java: 14 // json: 7
🌐
Medium
medium.com › @beckerjustin3537 › javascript-using-array-iteration-methods-on-a-json-file-with-nested-objects-2c66b25df91e
JavaScript: Using Array Iteration Methods on a JSON file with Nested Objects | by Justin Becker | Medium
May 16, 2023 - In this blog post I will go over ... my phase-1 of Flatiron. The forEach() method allows you to iterate over each element in an array and apply a function to each element....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-json-object-in-javascript
How to Iterate JSON Object in JavaScript? - GeeksforGeeks
July 23, 2025 - Object.keys(obj) returns the keys of the JSON object. Inside the forEach loop, each property value can be accessed using obj[key] JavaScript · const obj = { "company": 'GeeksforGeeks', "contact": '+91-9876543210', "city": 'Noida' }; Object.keys(obj).forEach(key => { console.log(`${key}: ${obj[key]}`); }); Output ·
🌐
CodePen
codepen.io › YannickFricke › pen › Vqzoob
JavaScript forEach example with JSON data
Minimize JavaScript Editor · Fold All · Unfold All · let html = ''; // JSON from json_encode($data) let json = [{"category_id":39,"chapters":[{"link":"http:\/\/google.de","chapter_name":"First chapter"}]},{"category_id":37,"chapters":[{"link":"http:\/\/google.de","chapter_name":"Second chapter"}]},{"category_id":42,"chapters":[{"link":"http:\/\/google.de","chapter_name":"Third chapter"}]}]; json.forEach(function(element) { let category_id = element.category_id; let chapters = element.chapters; console.log(category_id); chapters.forEach(function(chapter) { html += '<a href="'+chapter.link+'">&raquo;Chapter '+chapter.chapter_name+'</a>' console.log(chapter.link); console.log(chapter.chapter_name); }) html += '<br />' }) console.log(html); document.getElementById('test').innerHTML = html; !
🌐
Coderwall
coderwall.com › p › _kakfa › javascript-iterate-through-object-keys-and-values
JavaScript iterate through object keys and values (Example)
June 26, 2023 - ObjectKit.forEach = function Object_forEach (object, callback) { for (var key in object) { if (object.hasOwnProperty(key)) callback(key, object[key]); } }; ... When retrieving several key-value pairs from an object in JavaScript, you might need ...
🌐
ServiceNow Community
servicenow.com › community › developer-forum › iterate-through-array-in-json-object › m-p › 2198844
Iterate Through Array in JSON Object - ServiceNow Community
October 17, 2018 - var responseBody = JSON.stringify(response.getBody()); var httpStatus = response.getStatusCode(); var parsed = JSON.parse(responseBody); for (i = 0; i < parsed.addresses.length; i++) { gs.print(parsed.addresses[i].address1); }
🌐
jq
jqlang.org › manual
jq 1.8 Manual
The expression exp as $x | ... means: for each value of expression exp, run the rest of the pipeline with the entire original input, and with $x set to that value. Thus as functions as something of a foreach loop.
🌐
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 - This process will typically consist of two steps: decoding the data to a native structure (such as an array or an object), then using one of JavaScript’s in-built methods to loop through that data structure. In this article, I’ll cover both steps, using plenty of runnable examples. JSON (JavaScript Object Notation) is a text-based format for data exchange, widely used in web applications for transmitting data.