How to extract values when keys with same name

how to extract value from JSON response body when multiple keys with same name.

I want to retrieve ID from the below Json Body response and to get Any one of the ID from the below response. Tried using the Findbetween and by going with path. is there any alternative option to get the ID?

 "condition": {
       "display": "Item 1",
        "resource": {
        "id": "18395181",
         "stage": [
		 ]
		 
"condition": {
       "display": "Item 2",
        "resource": {
        "id": "18395182",
         "stage": [
		 ]
"condition": {
       "display": "Item 3",
        "resource": {
        "id": "18395183",
         "stage": [
		 ]
"condition": {
       "display": "Item 4",
        "resource": {
        "id": "18395184",
         "stage": [
		 ]
"condition": {
       "display": null,
        "resource": {
        "id": "18395185",
         "stage": [
		 ]

Hi @Arjun,

The JSON you’ve provided here isn’t actually valid. Below is a script that demonstrates using map against an array, as well as findBetween when operating on a string representation of it (findBetween has an optional flag that indicates whether the extraction should be repeated multiple times, in which case it will return an array of extracted values):

import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

export default function () {
  const array = [
    {
      "condition": {
        "display": "Item 2",
        "resource": {
          "id": "18395182",
          "stage": [
          ]
        },
      },
    }, {
      "condition": {
        "display": "Item 3",
        "resource": {
          "id": "18395183",
          "stage": [
          ],
        },
      },
    }, {
      "condition": {
        "display": "Item 4",
        "resource": {
          "id": "18395184",
          "stage": [
          ],
        },
      },
    }, {
      "condition": {
        "display": null,
        "resource": {
          "id": "18395185",
          "stage": [
          ],
        },
      },
    }
  ];
  const arrayAsString = JSON.stringify(array);

  // use map to reduce the array to an array of ids:
  let ids = array.map((item) => item.condition.resource.id);
  console.log(ids); // ["18395182", "18395183", "18395184", "18395185"]

  // use findBetween when dealing with strings:
  ids = findBetween(arrayAsString, '"id":"', '"', true); // 'true' here means repeat the extraction and return an array containing all of the matches
  console.log(ids); // ["18395182", "18395183", "18395184", "18395185"]
}

If the response is valid JSON, you can also use the json() function on the response and specify a GJSON selector.