How to extract data from a JSON payload

rob_kling
Tera Contributor

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 : 

 var a = JSON.stringify(responseBody);
gs.log("Policy Search policies.policyNumber : " + a.policies.policyNumber);
 
Result - Policy Search policies.policyNumber : undefined

 

 {
"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"
}
}
]
}       

2 ACCEPTED SOLUTIONS

Barrilito van D
Kilo Guru

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!

View solution in original post

Chetan Mahajan
Kilo Sage
Kilo Sage

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

View solution in original post

5 REPLIES 5

Barrilito van D
Kilo Guru

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!

Sandeep Rajput
Tera Patron
Tera Patron

@rob_kling Update your code as follows.

 

 

 var a = JSON.stringify(responseBody);
gs.log("Policy Search policies.policyNumber : " + a.policies[0].policyNumber);
gs.log("Policy Search policies.certificateNumber : " + a.policies[0].certificateNumber);

 

In your JSON, policies is an array hence it needs to be accessed via an index. Use a for look if there are more than one policy object in your policies array.

 

Also, for the address and broker fields, since they are not array type object, they can be accessed as follows.

 

 

gs.log("Policy address : " + a.address.streetAddress);

gs.log("Policy broker : " + a.broker.brokerCode);

 

 

Hope this help,s

I was having difficulty with the stringify command returning values, Barrilito's solution worked, once the project is implemented, I will go back and see why  stringify didn't work   

Chetan Mahajan
Kilo Sage
Kilo Sage

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