javascriptjsonnestednested-json

How to elegantly get a particular value from highly nested json object in JavaScript?


This is my json object by name data

{
"data": {
    "id": "---------",
    "type": "licenses",
    "attributes": {
        "name": "Floating Point Lic",
        "key": "-----------",
        "expiry": "2022-07-30T19:11:30.738Z",
        "status": "ACTIVE",
        "uses": 0,
        "suspended": false,
        "scheme": "ED25519_SIGN",
        "encrypted": false,
        "strict": true,
        "floating": true,
        "protected": true,
        "maxMachines": 10,
        "maxProcesses": null,
        "maxCores": null,
        "maxUses": null,
        "requireHeartbeat": false,
        "requireCheckIn": false,
        "lastValidated": "2022-07-07T08:33:58.195Z",
        "lastCheckIn": null,
        "nextCheckIn": null,
        "metadata": {         
            "role": "sde"
        },
        "created": "2022-06-30T19:11:30.736Z",
        "updated": "2022-07-07T08:33:58.199Z"
    },
    "relationships": {
        "account": {
            "links": {
                "related": "------------"
            },
            "data": {
                "type": "accounts",
                "id": "------------"
            }
        },
        "product": {
            "links": {
                "related": "------------------"
            },
            "data": {
                "type": "products",
                "id": "This I need"
            }
        },
        "policy": {
            "links": {
                "related": "---------"
            },
            "data": {
                "type": "policies",
                "id": "---------------"
            }
        },
        "group": {
            "links": {
                "related": "------------"
            },
            "data": null
        },
        "user": {
            "links": {
                "related": "---------------"
            },
            "data": null
        },
        "machines": {
            "links": {
                "related": "-------------------"
            },
            "meta": {
                "cores": 0,
                "count": 0
            }
        },
        "tokens": {
            "links": {
                "related": "----------------"
            }
        },
        "entitlements": {
            "links": {
                "related": "------------------"
            }
        }
    },
    "links": {
        "self": "-------------"
    }
},
"meta": {
    "ts": "2022-07-07T08:33:58.204Z",
    "valid": false,
    "detail": "must have at least 1 associated machine",
    "constant": "NO_MACHINES"
}

}

Their is data then their is relationships then product then data then id which I want

I am currently getting it by writing r['data']['data']['relationships']['product']['data']['id']

r here is this object but this does not looks elegant at all. Is their any way to fetch this in a better manner?


Solution

  • Use dots instead of brackets:

    const wantedId = r.data.relationships.product.data.id;
    

    Brackets are only useful if you're using dynamic references to properties.