Home > Software design >  Looping through nested JSON returns NULL
Looping through nested JSON returns NULL

Time:12-05

I'm trying to better understand how to work with nested JSON objects in JavaScript/React. I am getting data through the GitLab API in the following form:

const merge_requests =    [
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "tes.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      },
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "test.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      },]

I want to loop through all objects(merge requests) in this JSON and create a new array with the merge_user.name.

console.log(merge_requests[0].merge_user.name);
console.log(merge_requests[1].merge_user.name);

The logs above return both the correct values. However, I cannot loop through the JSON to create a new array from the data like this:

  const arrTest = [];

  for(var i = 0; i < Object.keys(merge_requests).length; i  )
  {
      var mergeUserName = merge_requests[i].merge_user.name;
      arrTest.push(mergeUserName);
  }

  console.log(arrTest);
}

The code above leads to the following error: Uncaught (in promise) TypeError: resultData[i].merge_user is null

Here is a picture: enter image description here

I am currently learning JS coming from R. I have huge problems working with JSON instead of dataframes and I cannot find any documentation to learn from. I would appreciated any advice/ sources.

CodePudding user response:

There is no need to use Object.keys(),you can use merge_requests.length directly

const arrTest = [];

for(var i = 0; i < merge_requests.length; i  ){
 let mergeUserName = merge_requests[i].merge_user.name;
 arrTest.push(mergeUserName);
}

console.log(arrTest);

const merge_requests =    [
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "tes.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      },
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "test.user",
          "name": "[email protected]",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      }]
      
const arrTest = [];

for(var i = 0; i < merge_requests.length; i  ){
 let mergeUserName = merge_requests[i].merge_user.name;
 arrTest.push(mergeUserName);
}

console.log(arrTest);

CodePudding user response:

const arrTest = [];

for(var i = 0; i < merge_requests.length; i  ){
 let mergeUserName = merge_requests[i].merge_user?.name;
 arrTest.push(mergeUserName);
}

console.log(arrTest);

merge_requests[i].merge_user?.name will return undefined if object is not present in the json.

CodePudding user response:

I copy & pasted your code & JSON and it works fine.
Make sure your JSON is parsed after getting it from ate API typeof merge_requests should return object, if it returns string then do the following: const parsedData = JSON.parse(merge_requests) and loop through parsedData

CodePudding user response:

i checked your code it's working fine.

Check your api request, are you sure you waiting for it till it get fulfilled?

  • Related