Jyoti Prakash Mallick
Jyoti Prakash Mallick

Reputation: 2074

How to retrieve value in javascript

I am returning a json as shown below,

{
    "entities": {
        "permissions": {
            "77de5140-9e1f-48b6-87a5-c80f12cd66d9": {
                "id": "77de5140-9e1f-48b6-87a5-c80f12cd66d9",
                "role": "ADMIN",
                "permissions": null,
                "canAccessAllAccounts": true,
                "allowedAccounts": null,
                "createdAt": "2022-01-30T18:20:46.901Z",
                "updatedAt": "2022-01-30T18:20:46.901Z",
                "deletedAt": null
            }
        },
        "users": {
            "9bba4c96-781b-4012-9a48-071c1cb5ec24": {
                "id": "9bba4c96-781b-4012-9a48-071c1cb5ec24",
                "username": "[email protected]",
                "activeAccountId": "a979189d-6bef-41f9-b224-892fbeb0955b",
                "enterpriseId": "9a69bba9-ed35-4589-8784-6b0e256bd7a0",
                "permissionId": "77de5140-9e1f-48b6-87a5-c80f12cd66d9",
                "firstName": "a48d1eb7270bb404",
                "lastName": "e0aaa6d09e19",
                "avatarUrl": null,
                "sendBookingRequestEmail": true,
                "isSSO": false,
                "createdAt": "2022-01-30T18:20:46.999Z",
                "updatedAt": "2022-01-30T18:20:46.999Z",
                "deletedAt": null,
                "permission": "77de5140-9e1f-48b6-87a5-c80f12cd66d9"
            }
        }
    },
    "result": "9bba4c96-781b-4012-9a48-071c1cb5ec24"
}

I am trying to get permissionsid value (it is occurred three places in the JSON), also forgot to mention in the original comment that these alphnumeric values in permissionid and userid are dynamics

When I am using the following, I am getting undefined

var res = JSON.stringify(response.body);
var userResponseParser = JSON.parse(res);
var permission_id = userResponseParser['permissionId'];

When I am using the following, I am getting Cannot read properties of undefined (reading 'id')

var res = JSON.stringify(response.body);
var userResponseParser = JSON.parse(res);
var permission_id = userResponseParser.entities.permissions[0].id;

When I am using the following, I am getting undefined

var res = JSON.stringify(response.body);
var userResponseParser = JSON.parse(res);
var permission_id = userResponseParser.entities.permissions[0];

When I am using the following, I am getting [object%20Object]

var res = JSON.stringify(response.body);
var userResponseParser = JSON.parse(res);
var permission_id = userResponseParser.entities.permissions;

What I am missing here, couldn't find same kind of question

Upvotes: 0

Views: 49

Answers (2)

Serge
Serge

Reputation: 43870

I see only 2 places, but in any cases remove this from your code

var res = JSON.stringify(response.body);
var userResponseParser = JSON.parse(res);

since your response data is parced already automatically

Since you can have several permissions or users , you can not get your data just using [0] or [1] since you don' t know how many users can be in the response. So try this code ( it was tested and working properly)

var permissionIds=[];
 Object.values(response.entities.permissions).forEach(element => {
   permissionIds.push(element.id)
 }); 

var userIds=[];
 Object.values(response.entities.users).forEach(element => {
   userIds.push(element.id)
 }); 

Upvotes: 0

mplungjan
mplungjan

Reputation: 177885

Why stringify and parse?

Anyway, it is more complex than you think

const obj = JSON.parse(str)

console.log(Object.values(obj.entities.users)[0].permissionId)
<script>
const str = `{
    "entities": {
        "permissions": {
            "77de5140-9e1f-48b6-87a5-c80f12cd66d9": {
                "id": "77de5140-9e1f-48b6-87a5-c80f12cd66d9",
                "role": "ADMIN",
                "permissions": null,
                "canAccessAllAccounts": true,
                "allowedAccounts": null,
                "createdAt": "2022-01-30T18:20:46.901Z",
                "updatedAt": "2022-01-30T18:20:46.901Z",
                "deletedAt": null
            }
        },
        "users": {
            "9bba4c96-781b-4012-9a48-071c1cb5ec24": {
                "id": "9bba4c96-781b-4012-9a48-071c1cb5ec24",
                "username": "[email protected]",
                "activeAccountId": "a979189d-6bef-41f9-b224-892fbeb0955b",
                "enterpriseId": "9a69bba9-ed35-4589-8784-6b0e256bd7a0",
                "permissionId": "77de5140-9e1f-48b6-87a5-c80f12cd66d9",
                "firstName": "a48d1eb7270bb404",
                "lastName": "e0aaa6d09e19",
                "avatarUrl": null,
                "sendBookingRequestEmail": true,
                "isSSO": false,
                "createdAt": "2022-01-30T18:20:46.999Z",
                "updatedAt": "2022-01-30T18:20:46.999Z",
                "deletedAt": null,
                "permission": "77de5140-9e1f-48b6-87a5-c80f12cd66d9"
            }
        }
    },
    "result": "9bba4c96-781b-4012-9a48-071c1cb5ec24"
}`</script>

Upvotes: 1

Related Questions