2

Have a dynamic value stringval How to extract values from it and store it in 3 different variable separated by pipes (|)

stringval ="A|B|C"

want to store these value separately in three different variables like Var1(value for A), Var2 (value for B), Var3 (value for C) . Let me know how to do it.

[
  {
   "Fieldid": "Fieldid/11",
    "fieldName": "TX.Sessionval.cost",
    "**stringval**": "jklah-dw-4c8d-8320-das313s3ASsda|000725N8WuUrfmIsbj!AS7alP|Danny_username"
  }
]

How to print only 3 Env variables? as mentioned in comment, need only 3

GPs
  • 67
  • 1
  • 10
  • Does this answer your question? [How can I convert a comma-separated string to an array?](https://stackoverflow.com/questions/2858121/how-can-i-convert-a-comma-separated-string-to-an-array) – Henke Feb 10 '21 at 13:00

1 Answers1

1

I would have thought you would need to get the value first:

let str = pm.response.json()[0].stringval

Then split that on the pipe:

let value = str.split('|')

Then store the values as variables:

pm.environment.set('value_1', value[0])
pm.environment.set('value_2', value[1]) 
pm.environment.set('value_3', value[2]) 

I've not run any of this so I would take each thing at a time and log it to the console to ensure that it's captured the right data points before putting it all together.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • adding to it .. for printing each value with and for rest of 3 values same way for 'value_1' ```console.log(pm.variables.get("value_1"))``` – GPs Aug 19 '20 at 17:21
  • You could show all 3 in the console by using `console.log(pm.environment.toObject())` – Danny Dainton Aug 19 '20 at 21:34
  • This will return all the variables in environment. only wanted newly created (val0 - val3) – GPs Aug 20 '20 at 18:36
  • 1
    Easy to filter that based on what the variable starts with - Many ways to do the same thing, you don't always have to add multiple statements that look the same Same can be said with the way I have set them in the answer. – Danny Dainton Aug 20 '20 at 19:38
  • Can we search this stringval ="A|B|C" from whole list and save it, new fields will be added in Json arraylist https://stackoverflow.com/questions/64145717/find-particular-object-value-from-json-object?noredirect=1#comment113453037_64145717 – GPs Oct 01 '20 at 15:35