`I got this response from the api call and I need to extract specific values from the response. Also, I want to use it as a pre request script in another api call.
- How can I extract values of the ids from the Json object? 2)Is it possible to extract particular value of the id key where key from equals "[email protected]"?
{
"content": [
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
},
{
"id": "8d547247-32d2-4553-b1fe-33b4ca00221d2",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: saas",
"inboxId": "af09331a-d681-48c4-9075-2c263f2dc5ba8",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T22:20:23.301Z",
"read": true,
"bodyExcerpt": "<div dir=\"auto\"><div dir=\"auto\"></div><p style=\"font-size:12.8px\">sid 325sd-df435-3fdgvd435-gdfv43</",
"teamAccess": true,
"bodyMD5Hash": "948B78E301880858EB66ABDE6698450B"
},
{
"id": "446760be-e261-441a-bffe-fa31aa935239",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Complete your registration",
"inboxId": "10ea0b7b-b5eb-4c0f-908d-394354324a71",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T07:41:41.685Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "3A7619478AB69B1F63C99B9716896B1B"
},
{
"id": "79a2c183-5b72-4bc1-98aa-63bf5d52c2e6",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dbdf5328",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T19:20:44.655Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "1ED8849F70CBCBBA6CF3CFEA0ACA66C4"
}
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 4,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"first": true,
"numberOfElements": 4,
"empty": false
}
`
CodePudding user response:
To parse json
, in JS there is a native way to do it.
JSON.parse()
Example of usage:
const data = JSON.parse(yourJsonObject);
then:
console.log(data.content)
in your case will return:
[
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
}, ...
]
CodePudding user response:
as you want to find id of the content with from that equals to "[email protected]" and content is an array of object so, you can use filter method of array like this.
const result=response.content.filter(value=>value.from=="[email protected]");
result variable will contain the whole object which match with your email so if you want only id then you can get it by using another map property.
const ids=result.map(item=>item.id);
CodePudding user response:
You can access to JSON object directly but if you want to convert to JS Object use JSON.parse()
//You can access to JSON object, but if you prefer to get JS Object do this: JSON.parse(objJSON)
const objJSON = {
"content": [
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
},
{
"id": "8d547247-32d2-4553-b1fe-33b4ca00221d2",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: saas",
"inboxId": "af09331a-d681-48c4-9075-2c263f2dc5ba8",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T22:20:23.301Z",
"read": true,
"bodyExcerpt": "<div dir=\"auto\"><div dir=\"auto\"></div><p style=\"font-size:12.8px\">sid 325sd-df435-3fdgvd435-gdfv43</",
"teamAccess": true,
"bodyMD5Hash": "948B78E301880858EB66ABDE6698450B"
},
{
"id": "446760be-e261-441a-bffe-fa31aa935239",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Complete your registration",
"inboxId": "10ea0b7b-b5eb-4c0f-908d-394354324a71",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T07:41:41.685Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "3A7619478AB69B1F63C99B9716896B1B"
},
{
"id": "79a2c183-5b72-4bc1-98aa-63bf5d52c2e6",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dbdf5328",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T19:20:44.655Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "1ED8849F70CBCBBA6CF3CFEA0ACA66C4"
}
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 4,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"first": true,
"numberOfElements": 4,
"empty": false
}
//Array of IDs of each content object
const arrayIDs = [];
objJSON.content.forEach( c => {
arrayIDs.push( c.id );
} )
//You can get the object filtering by the attribute you need
const rahulSharma = objJSON.content.find( c => {
return c.from == "[email protected]"
} )
//Then you can access to any attribute. In this case: id
console.log(`Sahul Sharma's ID: ${rahulSharma.id}`)
console.log('All IDs')
console.log(arrayIDs)
console.log('Info of Rahul Sharma')
console.log(rahulSharma)