Prashant Vadher
Prashant Vadher

Reputation: 11

Unable to extract value from the Response in Postman

I want to extract id value from the following response body.

   [
        {
            "id": "6311ebc04318ce16bedd1a14",
            "creationMethod": "board-creation",
            "name": "workspace18669413",
            "credits": [],
            "displayName": "Prashant Vadher's workspace",
            "desc": "",
            "descData": {
                "emoji": {}
            },
            "domainName": "epam.com",
            "idBoards": [],
            "idEnterprise": null,
            "idMemberCreator": "6310461591394a03d2012ca3",
            "invited": false,
            "invitations": [],
            "limits": {
                "orgs": {
                    "totalMembersPerOrg": {
                        "status": "ok",
                        "disableAt": 4000,
                        "warnAt": 3200
                    },
                    "freeBoardsPerOrg": {
                        "status": "ok",
                        "disableAt": 10,
                        "warnAt": 3
                    }
                }
            },
            "memberships": [
                {
                    "idMember": "6310461591394a03d2012ca3",
                    "memberType": "admin",
                    "unconfirmed": false,
                    "deactivated": false,
                    "id": "6311ebc04318ce16bedd1a16"
                }
            ],
            "membersCount": 1,
            "prefs": {
                "permissionLevel": "private",
                "orgInviteRestrict": [],
                "boardInviteRestrict": "any",
                "externalMembersDisabled": false,
                "associatedDomain": null,
                "googleAppsVersion": 1,
                "boardVisibilityRestrict": {
                    "private": "org",
                    "org": "org",
                    "enterprise": "org",
                    "public": "org"
                },
                "boardDeleteRestrict": {
                    "private": "org",
                    "org": "org",
                    "enterprise": "org",
                    "public": "org"
                },
                "attachmentRestrictions": null
            },
            "powerUps": [],
            "products": [],
            "billableMemberCount": 1,
            "activeBillableMemberCount": 1,
            "billableCollaboratorCount": 0,
            "url": "https://trello.com/workspace18669413",
            "website": null,
            "logoHash": null,
            "logoUrl": null,
            "premiumFeatures": [
                "additionalBoardBackgrounds",
                "additionalStickers",
                "customBoardBackgrounds",
                "customEmoji",
                "customStickers",
                "plugins"
            ],
            "promotions": [],
            "enterpriseJoinRequest": {},
            "standardVariation": null,
            "availableLicenseCount": null,
            "maximumLicenseCount": null,
            "ixUpdate": null,
            "teamType": null,
            "canEditDomain": true
        },
        {
            "id": "6310461e34b26a003d4f65d9",
            "creationMethod": null,
            "name": "userworkspace78017157",
            "credits": [],
            "displayName": "Trello Workspace",
            "desc": "",
            "descData": {
                "emoji": {}
            },
            "domainName": "epam.com",
            "idBoards": [
                "6311d55c9640b1010ee3a9e6",
                "6311e8216b6cd7003e656e5c",
                "6311ec42e22f75004513983e",
                "6311f1b3b36e0c007e68ccbb"
            ],
            "idEnterprise": null,
            "idMemberCreator": "6310461591394a03d2012ca3",
            "invited": false,
            "invitations": [],
            "limits": {
                "orgs": {
                    "totalMembersPerOrg": {
                        "status": "ok",
                        "disableAt": 4000,
                        "warnAt": 3200
                    },
                    "freeBoardsPerOrg": {
                        "status": "warn",
                        "disableAt": 10,
                        "warnAt": 3,
                        "count": 3
                    }
                }
            },
            "memberships": [
                {
                    "idMember": "6310461591394a03d2012ca3",
                    "memberType": "admin",
                    "unconfirmed": false,
                    "deactivated": false,
                    "id": "6310461e34b26a003d4f65db"
                }
            ],
            "membersCount": 1,
            "prefs": {
                "permissionLevel": "private",
                "orgInviteRestrict": [],
                "boardInviteRestrict": "any",
                "externalMembersDisabled": false,
                "associatedDomain": null,
                "googleAppsVersion": 1,
                "boardVisibilityRestrict": {
                    "private": "org",
                    "org": "org",
                    "enterprise": "org",
                    "public": "org"
                },
                "boardDeleteRestrict": {
                    "private": "org",
                    "org": "org",
                    "enterprise": "org",
                    "public": "org"
                },
                "attachmentRestrictions": null
            },
            "powerUps": [],
            "products": [],
            "billableMemberCount": 1,
            "activeBillableMemberCount": 1,
            "billableCollaboratorCount": 0,
            "url": "https://trello.com/userworkspace78017157",
            "website": null,
            "logoHash": null,
            "logoUrl": null,
            "premiumFeatures": [
                "additionalBoardBackgrounds",
                "additionalStickers",
                "customBoardBackgrounds",
                "customEmoji",
                "customStickers",
                "plugins"
            ],
            "promotions": [],
            "enterpriseJoinRequest": {},
            "standardVariation": null,
            "availableLicenseCount": null,
            "maximumLicenseCount": null,
            "ixUpdate": "6",
            "teamType": null,
            "canEditDomain": true
        }
    ]

I have tried following code:

*jsonData = JSON.parse(responseBody)
console.log("JSON Body - " + JSON.stringify(jsonData))
pm.environment.set('idOrganization', JSON.stringify(jsonData.id))
console.log("idOrganization: " + JSON.stringify(jsonData.id))*

But getting undefined value in console. idOrganization: undefined

Is there any other way to save this value in environment variable ?

Upvotes: 0

Views: 81

Answers (2)

Jyoti Prakash Mallick
Jyoti Prakash Mallick

Reputation: 2076

Guess your requirement is unclear, there are two id's present, which one you want to retrieve? If any specific it is already answered.

If you want both with comma separated, then please try this.

 const responseBODY = pm.response.json();
let idValue = "";
for (let i=0; i < responseBODY.length; i++) {
            idValue += responseBODY[i].id+",";
        }
finalIDValue = idValue.substring(0,idValue.length - 1); 
//put your logic how you want to use it
pm.environment.set("idOrganization", finalIDValue);

Upvotes: 1

ra_tester
ra_tester

Reputation: 252

could you try such:

const responseJson = pm.response.json();

pm.environment.set("idOrganization", responseJson[0]["id"]);

this is for the first item from your array.

Upvotes: 2

Related Questions