GPs
GPs

Reputation: 85

How to extract the string values from json array and store it

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

Upvotes: 2

Views: 227

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25921

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.

Upvotes: 1

Related Questions