Liyas Thomas
Liyas Thomas

Reputation: 23

Iterate through an array of objects with deep nested objects and arrays

I've a JSON array which is generated from a Collection > Folder/Request > Request structure.

Sample:

[
  {
    "name": "My Collection",
    "folders": [
      {
        "name": "bdg",
        "requests": [
          {
            "url": "https://reqres.in",
            "path": "/api/users",
            "method": "GET",
            "auth": "None",
            "httpUser": "",
            "httpPassword": "",
            "passwordFieldType": "password",
            "bearerToken": "",
            "headers": [],
            "params": [],
            "bodyParams": [],
            "rawParams": "",
            "rawInput": false,
            "contentType": "application/json",
            "requestType": "cURL",
            "name": "My Request",
            "collection": 0
          }
        ]
      }
    ],
    "requests": [
      {
        "url": "https://reqres.in",
        "path": "/api/users",
        "method": "GET",
        "auth": "None",
        "httpUser": "",
        "httpPassword": "",
        "passwordFieldType": "password",
        "bearerToken": "",
        "headers": [],
        "params": [],
        "bodyParams": [],
        "rawParams": "",
        "rawInput": false,
        "contentType": "application/json",
        "requestType": "cURL",
        "name": "My Request",
        "collection": 0
      }
    ]
  },
  {
    "name": "fndo",
    "folders": [
      {
        "name": "bdghg",
        "requests": [
          {
            "url": "https://reqres.in",
            "path": "/api/users",
            "method": "GET",
            "auth": "None",
            "httpUser": "",
            "httpPassword": "",
            "passwordFieldType": "password",
            "bearerToken": "",
            "headers": [],
            "params": [],
            "bodyParams": [],
            "rawParams": "",
            "rawInput": false,
            "contentType": "application/json",
            "requestType": "cURL",
            "name": "My Request",
            "collection": 1
          }
        ]
      }
    ],
    "requests": [
      {
        "url": "https://reqres.in",
        "path": "/api/users",
        "method": "GET",
        "auth": "None",
        "httpUser": "",
        "httpPassword": "",
        "passwordFieldType": "password",
        "bearerToken": "",
        "headers": [],
        "params": [],
        "bodyParams": [],
        "rawParams": "",
        "rawInput": false,
        "contentType": "application/json",
        "requestType": "cURL",
        "name": "My Request",
        "collection": 1
      }
    ]
  }
]

I want to iterate through all nodes, arrays and objects inside this JSON. The JSON will be consisting of objects, array of objects and arrays.

I tried Object.keys(), .map() and .forEach() but failed in nested conditions.

An E6S solution with recursion will be a life saver.

Thank in advance!

Upvotes: 2

Views: 813

Answers (2)

Liyas Thomas
Liyas Thomas

Reputation: 23

@codingspear 's answer is exactly I was looking for https://stackoverflow.com/a/59441109/8335089

I was able to iterate through all node with this:

      let collections = JSON.parse(myJson);
      for (let i = 0; i < collections.length; i++) {
        console.log("Collection", i + 1, collections[i].name);
        let folders = collections[i].folders;
        for (let i = 0; i < folders.length; i++) {
          console.log("Folder", i + 1, folders[i].name);
          let requests = collections[i].requests;
          for (let i = 0; i < requests.length; i++) {
            console.log("Request", i + 1, requests[i].name);
          }
        }
        let requests = collections[i].requests;
        for (let i = 0; i < requests.length; i++) {
          console.log("Request", i + 1, requests[i].name);
        }
      }

Upvotes: 0

CodingSpear
CodingSpear

Reputation: 36

If the aim is to make this JSON into an object then the best solution is to parse the JSON. For example:

let myJson = "[
  {
    "name": "Collection 1",
  },
  {
    "name": "Collection 2",
  },
  {
    "name": "Collection 3",
  }
]"

The next step is to use the JSON.parse() function

let myObj = JSON.parse(myJson);

Now you can loop easily if you want

for(let i = 0; i < myObj.length; i++){
  console.log(myObj[i].name);
}

Or just access any of the properties directly

let name1 = myObj[0].name;

Upvotes: 1

Related Questions