I have below JSON response from the request. I want to extract value term_no from the ticket list key json response. Tried using json path finder and not getting the correct value. Could some one help me to get the correct output.
Json Response
{
"data": {
"action": "vcvc",
"method": "xcxcx",
"content": "{"action":"wdsds","ticket_version":"23343","payment_version":"3323","purchase_version":"5656","responselist":[{"seq_no":"1000466303","bank_resp_code":"0","transid":"27344650","auth_code":"123456","amount":"210","paymethod_id":"*****","payment_no":""}],"ticketlist":[{"seq_no":"1000882730","trx_no":"83149","term_no":"2001","sale_no":"1","ticket_no":"1","ticket_amount":"210","start_validity":"20240127025741","end_validity":"20250126025741","tt_id":"12010","printed_serial_number":"8706","trx_seq_id":"1100430687","bus_zone":"9","active_ticket_duration":"165","exception_data":"","zip_code":"07001"},{"seq_no":"1000882731","trx_no":"83149","term_no":"2001","sale_no":"1","ticket_no":"1","ticket_amount":"0","start_validity":"xxxxx","end_validity":"20250126025741","tt_id":"12010","printed_serial_number":"xxx","trx_seq_id":"xxxxxx","bus_zone":"x","active_ticket_duration":"xxx","exception_data":"","zip_code":"07001"}],"status_msg":"Success","status_code":"0","version":"1.0","site_id":"1_N","term_no":"2001"}",
"status_msg": "Success",
"status_code": "0",
"version": "1.0",
"site_id": "1_N"
}
}
Used below way
pm.test("The response has all properties", () => {
const responseJson = pm.response.json();
pm.expect(responseJson.data.content.ticketlist[0].term_no).to.eql('2001');
});
Getting the below Error
The response has all properties | JSONError: Unexpected token ‘o’ at 1:2 [object Object]
3
Answers
The content field itself is JSON string encoded into another json string. So we just need to decode that again after the initial decode.
If
responseJson
is an already parsed json string converted into object, then this is what you want:If
responseJson
is the raw string from the server that needs to be parsed into an object, then this:And I just noticed you are trying to read
ticketlist
off ofaction
. That’s not the hierarchy of your object. So ifresponseJson
is fully parsed, you might have meant this:Also of note, your code here:
Wouldn’t work even if responseJson was fully parsed. It would be this:
The problem is with the content object being a string. So you will need to parse the initial JSON response using the pm.response.json() function, and then use JSON.parse to parse the content string.
Why you have two objects in the ticketlist array with nearly identical information is another question completely.
The
content
of thedata
is a JSON string. You will need to parse it, prior to accessing the innerticketlist
array.