- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 09:02 AM
I am to extract data from this JSON payload , have been able to parse and stringify it but cannot access the fields within. Setting up log statements all I am returning is undefined
For example :
{
"status": 200,
"policies": [
{
"policyNumber": "1234567",
"certificateNumber": "12345678",
"businessLine": "C",
"businessType": "P",
"onOffSystemCode": "1",
"companyNumber": 1,
"policyStatusCode": "A",
"keyClientFlag": "Y",
"creationDate": null,
"createdBy": 0,
"lastUpdateDate": null,
"lastUpdatedBy": 0,
"policyIndexId": 2617551,
"staffPolicyFlag": "N",
"sourceSystemCode": "XXX",
"conversionEffectiveDate": null,
"policyEffectiveDate": null,
"policyExpiryDate": null,
"underwriterBranchNumber": 0,
"underwriterBranchName": null,
"ConversionFromNRGEffDt": null,
"PolicyPrintTypeCd": "16",
"CommAutoSubTypeCd": null,
"customerDetails": {
"firstName": null,
"middleName": null,
"lastNameBusinessName": "ACME MANUFACTURING"
},
"address": {
"streetAddress": "11 ANY STREET PO BOX 123",
"cityProvince": "ANYTOWN,ON",
"postalCode": "A1A 1A1"
},
"broker": {
"brokerCode": "1234",
"brokerName": "ACME INSURANCE BROKERS INC"
}
}
]
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 12:05 AM
Hi,
It does not work because in your responsebody object there is an aray (noted by the [] after policies). So each policie is an array. As there is only one in this example you should refer to this policy as it is the first in the array so it has index "0" and you can refer to it by using the "[0]" syntax, so you can fetch your value using:
console.log("Policy Search policies.policyNumber : " + responseBody.policies[0].policyNumber);
It will output:
Policy Search policies.policyNumber : 1234567
If you think my answer put you in the right direction or you appreciated my effort, please mark the response as Helpful (👍 )or mark my response as Correct (✅).
Thanks in advance! Good luck!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 01:29 AM
Hello @rob_kling ,
It looks like you're trying to access the fields within a JSON payload. Before you can access the properties, you need to parse the JSON string into an actual object. Here's the correct way to extract data from the JSON payload:
var responseBody = /* Your JSON payload here */;
var a = JSON.parse(responseBody);
// Now you can access the properties within the parsed object
var policyNumber = a.policies[0].policyNumber;
var lastNameBusinessName = a.policies[0].customerDetails.lastNameBusinessName;
var streetAddress = a.policies[0].address.streetAddress;
var brokerCode = a.policies[0].broker.brokerCode;
gs.log("Policy Number: " + policyNumber);
gs.log("Last Name/Business Name: " + lastNameBusinessName);
gs.log("Street Address: " + streetAddress);
gs.log("Broker Code: " + brokerCode);
Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 01:30 PM
This was similar to Barrilito's and will work