0

Currently I'm working with writing test script that can read and compare the values return from api and json file I provided. The problem is that I'm not sure the error is in the script or json file.

error returning : JSONError: Unexpected token u in JSON at position 0

json file name (Book1.json)
[
    {
      "name": "Test Case 1",
      "tests": [
        {
          "testName": "Test 1",
          "location": {
            "row": 5,
            "column": 12
          },
          "expectedResult": "1023.00"
        },
        {
          "testName": "Test 2",
          "location": {
            "row": 7,
            "column": 10
          },
          "expectedResult": "500.50"
        }
      ]
    },
    {
      "name": "Test Case 2",
      "tests": [
        {
          "testName": "Test 3",
          "location": {
            "row": 2,
            "column": 8
          },
          "expectedResult": "750.25"
        }
      ]
    }
  ]

Test script :-

const responseJson = pm.response.json();
var data = JSON.parse(responseBody);
var investmentLink = data.result;

// Write a script that can read the JSON file provided and compare the actual and expected results
const jsonData = JSON.parse(pm.iterationData.get("Book1.json"));

// Write a script that can read the JSON file provided and compare the actual and expected results
function performTests(testCase) {
  const tests = testCase.tests;

  tests.forEach((test) => {
    const testName = test.testName;
    const location = test.location;
    const expectedResult = test.expectedResult;
    const row = location.row;
    const column = location.column;

    const actualData = investmentLink.reportPIIllustrationTable[0].illustration[row - 1][`column${column}`].data;
    pm.test(testName, () => {
      pm.expect(actualData).to.equal(expectedResult);
    });
  });
}

jsonData.forEach((testCase) => {
  const testCaseName = testCase.name;
  pm.test(testCaseName, () => {
    performTests(testCase);
  });
});

json file preview

As I look into the error it shows something might wrong with the json file. Someone may help with this issue

Rhowin
  • 1
  • 1
  • Difficult to tell. But open up your json file and validate in vscode, if you have. Also execute curl and store result in file and carefully validate data. Check header for confirmation – Popeye Jul 18 '23 at 03:54
  • at what line do you get that parse error? seems like JSON cant parse the data you are passing it to. Make sure your types are correct in the parser. Also maybe try to stringify the book object then parse? or maybe its already a json then it dont need to be parsed into json anymore? – Dumidor Dumbleplex Jul 18 '23 at 05:48

0 Answers0