无法在Postman中使用jsonpath表达式

nfeuvbwi  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(281)

我有这样的Json,我尝试通过使用JSonpath表达式打印一个我想要的值,详细信息是我想打印id = 10的用户的first_name

{
    "page": 2,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 7,
            "email": "michael.lawson@reqres.in",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": "https://reqres.in/img/faces/7-image.jpg"
        },
        {
            "id": 8,
            "email": "lindsay.ferguson@reqres.in",
            "first_name": "Lindsay",
            "last_name": "Ferguson",
            "avatar": "https://reqres.in/img/faces/8-image.jpg"
        },
        {
            "id": 9,
            "email": "tobias.funke@reqres.in",
            "first_name": "Tobias",
            "last_name": "Funke",
            "avatar": "https://reqres.in/img/faces/9-image.jpg"
        },
        {
            "id": 10,
            "email": "byron.fields@reqres.in",
            "first_name": "Byron",
            "last_name": "Fields",
            "avatar": "https://reqres.in/img/faces/10-image.jpg"
        },
        {
            "id": 11,
            "email": "george.edwards@reqres.in",
            "first_name": "George",
            "last_name": "Edwards",
            "avatar": "https://reqres.in/img/faces/11-image.jpg"
        },
        {
            "id": 12,
            "email": "rachel.howell@reqres.in",
            "first_name": "Rachel",
            "last_name": "Howell",
            "avatar": "https://reqres.in/img/faces/12-image.jpg"
        }
    ],
    "support": {
        "url": "https://reqres.in/#support-heading",
        "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
    }
}

我在postman的测试中编写了一个简单的脚本来实现这一点

var jsonData = pm.response.json();
let x = jsonData.data[?(@.id==8)].first_name)
console.log(x)

但在测试中的React是

There was an error in evaluating the test script:  SyntaxError: Unexpected token '?'

the result as like this picture

nnt7mjpx

nnt7mjpx1#

您可以使用find()来查找与您的条件匹配的对象。

var jsonData = pm.response.json();
let x = jsonData.data.find(item => item.id === 8).first_name;
console.log(x);
//Lindsay

相关问题