Deeply Nested Object

I have a REST API that provides a deeply nested JSON file.

I keep running into the issue at the 5th level that I receive an error.
When I add .color after .current_status I get an error.

Hey @Justin1112, what does batch_projects.data look like? It's difficult to diagnose the issue without seeing the structure of the original JSON.

{
  "data": [
    {
      "body": {
        "data": {
          "gid": "1204393417366992",
          "archived": false,
          "color": null,
          "completed": false,
          "completed_at": null,
          "created_at": "2023-04-13T18:33:41.231Z",
          "current_status": null,
          "current_status_update": null,
          "custom_fields": [
            {
              "gid": "1204303494304146",
              "enabled": true,
              "enum_options": [
                {
                  "gid": "1204305432448940",
                  "color": "yellow-orange",
                  "enabled": true,
                  "name": "1. In Progress SRA",
                  "resource_type": "enum_option"
                },

Thanks @Justin1112. I'm not sure what it is you want to do, nothing is nested under current_status?

If you want to access the color value between archived and completed, you just want data.body.data.color.

I provided a sample that didn't have data in the "current_status" field. Here is the example.

Under "current_status" there is "color". This is one of the values that I want to pull out however, I receive an error when using
new_obj['color'] = data[key]["data"]["body"]["data"]["current_status"].color
or

new_obj['color'] = data[key]data.body.data.current_status.color

or
new_obj['color'] = data[key]['data']['body']['data']['current_status'].color
or
new_obj['color'] = data[key]["data"]["body"]["data"]["current_status"]["color"]

      "body": {
        "data": {
          "gid": "1204355355948180",
          "archived": false,
          "color": null,
          "completed": false,
          "completed_at": null,
          "created_at": "2023-04-07T16:15:17.814Z",
          "current_status": {
            "gid": "1204355355948194",
            "author": {
              "gid": "1203990438815410",
              "name": "Johnny Bob",
              "resource_type": "user"
            },
            "color": "green",
            "created_at": "2023-04-07T16:17:12.456Z",
            "created_by": {
              "gid": "1203990438815410",
              "name": "Johnny Bob",
              "resource_type": "user"
            },
            "modified_at": "2023-04-07T16:17:12.456Z",
            "resource_type": "project_status",
            "text": "Summary\my updates",
            "title": "Status Update - Apr 7"
          },
          "current_status_update": {
            "gid": "1204355355948194",
            "resource_type": "status_update",
            "resource_subtype": "project_status_update",
            "title": "Status Update - Apr 7"

If the 1st example you provided is a valid example then you're JS will fail on hitting that example as it won't be able to find the child property of current status as it doesn't exist.

Assign a current status to a variable then check for null/undefined. If it is, handle that how you need, if not then delve deeper and get the colour you need.

Just want to toss in that optional chaining might be particularly useful here as well! Something like: data[key]?.["data"]?.["body"]?.["data"]?.["current_status"]?.color

1 Like