0

I need to select any random value from the below json every time I run the code. How can I achieve this in Karate? I need to get any random value and use in another feature file.


[
  {
    "derivativeId": "BL33F1-2020-MASMGM2-GPKBPKB-GPSAPSA-GPS1PS1-GPTEPTE-GPU2PU2-GWW2WW2-GYOBYOB"
  },
  {
    "derivativeId": "1233AV-2020-GPJCPJC-GPRAPRA-GYOIYOI-MLSE8RL"
  },
  {
    "derivativeId": "1239TX-2020-GYOIYOI"
  },
  {
    "derivativeId": "1233AV-2020-GPJCPJC-GPRAPRA-MSNRSA6-GW4CW4C-GYOIYOI-MLSE8RL"
  },
  {
    "derivativeId": "1233AV-2020-GPDWPDW-GPRAPRA-GW4CW4C-GYOIYOI-MLSE8RL"
  }
]

Nicktar
  • 5,548
  • 1
  • 28
  • 43
user3276076
  • 11
  • 1
  • 3
  • Welcome to stackoverflow.com. Please consult [ask] on how to ask good and answerable questions. Also, what have you tried before? Why didn't that work? Where's your code? Also never upvoting or accepting answers will not help you to attract more people to answer your questions. – Nicktar Feb 27 '20 at 09:48
  • As you are new to StackOverflow, Please search in StackOverflow first with karate tag as someone might have asked a similar or same question and it has already been answered. Please refer this answer: https://stackoverflow.com/a/53975071/442595 – Neodawn Feb 27 '20 at 09:54

1 Answers1

2

Here you go, also refer: Karate - Select a random element from json

* def random = function(max){ return Math.floor(Math.random() * max) }
* def index = random(response.length)
* def result = response[index].derivativeId
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248