Searching JSON response

Hello,

I have a API response where I want to search for a entry and validate against the value in my data sheet. The below tests work fine to do that except that if one of the entry does not get list the test will not highlight the missing entry.

Test flow:

  1. Search the Entire array where to find a block that contains ACCOUNTTITLE = “Inventory” and TR_TYPE=1 within it
  2. If the above combination is not found in the response notify the missing entry

Any suggestion would be helpful. Thanks in advance.

JSON Response:

 [
        {
            "BATCHNO": "1",
            "ENTRY_DATE": "02/01/2020",
            "TR_TYPE": "1",
            "ACCOUNTNO": "1400",
            "ACCOUNTTITLE": "Inventory",
            "TRX_AMOUNT": "716.11",
            "AMOUNT": "776.82"
        },
        {
            "BATCHNO": "1",
            "ENTRY_DATE": "02/01/2020",
            "TR_TYPE": "1",
            "ACCOUNTNO": "5005",
            "ACCOUNTTITLE": "Purchase Tax Paid",
            "TRX_AMOUNT": "143.22",
            "AMOUNT": "155.36"
        },
        {
            "BATCHNO": "1",
            "ENTRY_DATE": "02/01/2020",
            "TR_TYPE": "-1",
            "ACCOUNTNO": "2000",
            "ACCOUNTTITLE": "Accounts Payable",
            "TRX_AMOUNT": "859.33",
            "AMOUNT": "932.18"
        }
    ]

**Tests:**

var jsonData = JSON.parse(responseBody); //Parse the JSON responseBody

var glAccounts = [data.je_sales_tax_credit, data.je_accs_payable_credit, data.je_purchase_tax_debit, data.je_purchase_tax_credit, data.jeInventoryDebit, data.jeInventoryCredit, data.jeInventoryHardwareCredit, data.jeInventoryHardwareDebit];

//Get the total count of the number of Journal Entries 
var jeCount = jsonData.length;

tests["Verify total number of Journal Entries listed  : " + jeCount] = jeCount === data.resultCount;

//Check if Inventory Account needs to be debited
if (data.jeInventoryDebit !== "")

    //Search through the API response for ACCOUNTTITLE
    for (je = 0; je < jeCount; je++) {

        var accountTitle = jsonData[je].ACCOUNTTITLE;
        var transactionType = jsonData[je].TR_TYPE;

        if (accountTitle == "Inventory" && transactionType == "1") {
            tests["Verify Inventory - Transaction Amount Debited : " + jsonData[je].TRX_AMOUNT] = jsonData[je].TRX_AMOUNT === "" + data.jeInventoryDebit + "";
            break;
        } 
   
    }

figured out a solution for my problem. Do let me know if there is any other way of doing this well.

Thanks

//Check if Sales Tax needs to be Credited
if (data.je_sales_tax_credit !== "") {

        pm.test("Verify Sales Tax Account Credit " + data.je_sales_tax_credit, function() {
                var result;
                for (je = 0; je < jeCount; je++) {
                        var accountTitle = jsonData[je].ACCOUNTTITLE;
                        var transactionType = jsonData[je].TR_TYPE;

                        //If element matching accountTitle and transactionType found      
                        if (accountTitle == "Sales Taxes Payable" && transactionType == "-1") {
                                result = true;
                                break;
                        }

                }
                pm.expect(true).to.eql(result);
                pm.expect(jsonData[je].TRX_AMOUNT).to.equal("" + data.je_sales_tax_credit + "");
        });
}

Hey @vjjohnson04, welcome to the community!

This should work if you’re trying to find a match and then break outside of the loop if that’s your intention, then sure. My suggested change, while not significant, would be to set the default value of result to false if you’re then changing it to true in your if statement when a match is found.

Thanks SabriH :slightly_smiling_face: