skip to Main Content

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


  1. 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:

    var term_no = JSON.parse(responseJson.data.content).ticketlist[0].term_no;
    

    If responseJson is the raw string from the server that needs to be parsed into an object, then this:

    var responseObj = JSON.parse(responseJson);
    var term_no = JSON.parse(responseObj.data.content).ticketlist[0].term_no;
    

    And I just noticed you are trying to read ticketlist off of action. That’s not the hierarchy of your object. So if responseJson is fully parsed, you might have meant this:

    Also of note, your code here:

    pm.expect(responseJson.data.content.action['0'].ticketlist.term_no).to.eql('2001');
    

    Wouldn’t work even if responseJson was fully parsed. It would be this:

    pm.expect(responseJson.data.content.ticketlist[0].term_no).to.eql('2001');
    
    Login or Signup to reply.
  2. 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.

    const responseJson = pm.response.json();
    
    pm.test("term_no eqls 2001", () => {
        let content = JSON.parse(responseJson.data.content)
        pm.expect(content.ticketlist[0].term_no).to.eql('2001');
    });
    

    Why you have two objects in the ticketlist array with nearly identical information is another question completely.

    Login or Signup to reply.
  3. The content of the data is a JSON string. You will need to parse it, prior to accessing the inner ticketlist array.

    const pm = testSuite();
    
    pm.test("The response has all properties", () => {
      const responseJson = pm.response.json();
      const content = JSON.parse(responseJson.data.content); // Parse JSON string
      pm.expect(content.ticketlist[0].term_no).to.eql('2001');
    });
    
    function testSuite() {
      return {
        response: {
          json: () => ({
            "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"
            }
          })
        },
        test(description, callback) {
          console.log(`[Test] ${description}`);
          callback();
        },
        expect(actualValue) {
          return {
            to: {
              eql(expectedValue) {
                if (actualValue !== expectedValue) {
                  throw new Error(`[Fail] Actual value '${actualValue}' does not match expected value '${expectedValue}'`);
                }
              }
            }
          };
        },
      };
    }
    .as-console-wrapper { top: 0; max-height: 100% !important; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search