You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman测试脚本读取JSON文件报JSONError: Unexpected token u求排查

排查Postman测试脚本JSON解析错误:JSONError: Unexpected token u in JSON at position 0

问题场景

编写Postman测试脚本读取Book1.json并与API返回值对比时,触发错误:JSONError: Unexpected token u in JSON at position 0。以下是相关文件和代码:

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

原测试脚本代码

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);
  });
});

错误原因分析

Unexpected token u 本质是JSON.parse的参数为undefined,对应脚本中pm.iterationData.get("Book1.json")未获取到有效数据,核心问题包括:

  • 错误将文件名作为迭代数据的键名:Postman导入迭代数据后,文件内容会直接作为迭代对象,而非以文件名作为键存储。
  • 冗余的JSON解析:pm.response.json()已经完成了响应体的解析,后续JSON.parse(responseBody)属于重复操作,可能引发逻辑冲突。

修复方案

步骤1:正确导入迭代数据

在Collection Runner中导入Book1.json作为迭代数据:

  • 打开Collection Runner,选择目标集合。
  • 在Data选项卡上传Book1.json,确认文件内容被解析为数组结构。

步骤2:修正脚本代码

// 简化响应解析,避免重复操作
const data = pm.response.json();
const investmentLink = data.result;

// 直接获取迭代数据(pm.iterationData已完成JSON解析,无需再次调用JSON.parse)
const jsonData = pm.iterationData.toArray();

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

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

可选方案:不使用迭代数据(直接读取文件)

若无需迭代执行,可将Book1.json内容存入环境变量(如testData),再修改脚本:

const data = pm.response.json();
const investmentLink = data.result;

// 从环境变量读取并解析JSON数据
const jsonData = JSON.parse(pm.environment.get("testData"));

// 后续测试逻辑同上

验证方法

  1. 运行Collection Runner,确认迭代数据已成功加载。
  2. 执行测试脚本,检查是否再触发JSON解析错误。
  3. 验证API返回值与预期结果的对比逻辑是否正常执行。

内容的提问来源于stack exchange,提问作者Rhowin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 03:08:22