-2

How to extract the ID of the first object from the response? I am getting the below response of the API.

{"data":[{"id":1,"description":"Test description 1", "Location": "test location 1"}, {"id":2,"description":"Test description 2", "Location": "test location 2"}, {"id":3,"description":"Test description 3", "Location": "test location 3"}]}

I want to extract the id= 1, from the above response. I have tried with {"id":(.+?), regular expression. But I am getting randomly any of the id. What is the regular expression to get the first id from the response?

Kris
  • 8,680
  • 4
  • 39
  • 67
vijayateke
  • 77
  • 1
  • 10

2 Answers2

2

To get what you want (bad idea):

Regex:

"id":(\d+),.*

Get the result with \1.

Test here.


To get what you need: use a proper json parser. Regexes are not suitable for handling complex stuff (also including HTML among many others).

virolino
  • 2,073
  • 5
  • 21
1

Use JSON Extractor instead of Regular expression extractor for the JSON response.

To extract the first id from the response you mentioned, use JSON path expressions as : .data[0].id

Example Screenshots:

enter image description here

enter image description here

Now, you can pass the variable test to your next API request as ${test}

Masud Jahan
  • 3,418
  • 2
  • 22
  • 35