-1

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?

1 Answers1

1

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.

Yoz
  • 41
  • 2
  • Brackets are also useful if there are special characters within the properties. Like `r["prop-1"]`. Don't think this needs an answers as there are many duplicates [JavaScript property access: dot notation vs. brackets?](https://stackoverflow.com/questions/4968406) and [How can I access and process nested objects, arrays, or JSON?](https://stackoverflow.com/questions/11922383) – adiga Jul 07 '22 at 08:55
  • Thanks Yoz But it does not make a difference as it still looks like a long phrase Plus I am actually using typescript so cannot use dots – KESHAV MAHESHWARI Jul 07 '22 at 09:09
  • @KESHAVMAHESHWARI if you have a nested object, you need to refer to those properties somehow. You can use dot notation if you define proper types or interfaces for the object – adiga Jul 07 '22 at 09:59