VSCode REST client - how to get value from array in response

186 views Asked by At

I'm trying to grab the value "141" from the response using the expression

@second = {{getall.response.body.[1].id}}

HTTP/1.1 200 OK
Connection: close
Content-Type: application/json; charset=utf-8
Date: Thu, 07 Dec 2023 09:20:21 GMT
Server: Kestrel
Transfer-Encoding: chunked

[
  {
    "id": 140,
    "name": "Orlov 6 uger",
    "createdDate": "2023-12-07T09:01:29.0030238",
    "lastModifiedDate": "2023-12-07T09:07:31.4865664"
  },
  {
    "id": 141,
    "name": "Ferie 6 uger- cloud",
    "createdDate": "2023-12-07T09:11:57.7474033",
    "lastModifiedDate": "2023-12-07T09:11:57.7474044"
  }
]

I can the first "id" without issue using

# @name getfirst
GET https://{{baseUrl}}/api/todos/140

@first = {{getfirst.response.body.id}}
###
GET https://httpbin.org/get?id={{first}}

But when I try to access 2 item in the array with @second = {{getall.response.body.[1].id}} using

# @name getall
GET https://{{baseUrl}}/api/todos

@second = {{getall.response.body.[1].id}}
###
GET https://httpbin.org/get?id={{second}}

the variable is not set


{
  "args": {
    "id": "{{getall.response.body.[1].id}}"
  },

How to I get @second to get the value "141"?

1

There are 1 answers

0
Jørgen Thyme On

And the answer is:

@second = {{getall.response.body.1.id}}

Not:

@second = {{getall.response.body.[1].id}}