简体   繁体   中英

Not able to get value from postman response body

I have an API which has response as below:

"message": "Success!",
    "user": {
        "id": 17,
        "first_name_kanji_or_hiragana": "Hiragana",
        "last_name_kanji_or_hiragana": "Name",
        "first_name_katakana": null,
        "last_name_katakana": null,
        "email": "user@example.com",
        "image_path": null,
        "email_verified_at": "2019-03-06 04:44:46",
        "type": "admin",
        "created_at": "2019-03-06 04:44:46",
        "updated_at": "2019-03-06 04:44:46",
        "deleted_at": null,
        "full_name": "Name Hiragana",
        "full_image_path": null,
        "roles": [

Each time when i am running the API new user id is generating. What i want to do is that i want to fetch the id each time i run the api & then want to use that id in next response. I have used below code to fetch the id but it's not working

pm.test(responseBody, true)
var jsonData = JSON.parse(responseBody); 
jsonData.data.user[0].id
pm.test(responseBody, true)
var jsonData = JSON.parse(responseBody); 
jsonData.data.user[0].id // youre access the user if its array its not

Just use jsonData.data.user.id or jsonData.data.user[id]

You are trying to access an array called user

jsonData.data.user[0].id

But user is an object, so you need to access: jsonData.data.user.id

There are sometimes that you need to check the complete json directly from your request.

user is not an array. Try the below one.

jsonData.data.user.id

Your solution will work when you have the JSON like below,

    "lib_folders": {
        "changed": false,
        "examined": 5,
        "failed": false,
        "files": [
            {
                "atime": 1549608521.3831923,
                "ctime": 1548742613.5470872,
                "path": "/root/abc/xyz",
                "xusr": true
            },
            {
                "atime": 1549608521.4031928,
                "ctime": 1548742462.3279672,
                "path": "/root/123/456",
                "xusr": true
            }
        ],
        "matched": 2,
        "msg": ""
    }

For the above JSON if you want to get the path of the first file, then use the below code,

jsonData.data.lib_folders.files[0].path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM