0

How to search whole jSON output from postman and get only string values from newFields array and save in variable based on fieldName only

fieldName": "TX.Sessionval.cost

Have to update Test case everytime since array index changes for newField with new build, next time the fieldid index would be /118 or /129 /234 .. so on

What does not change is fieldName: it remains same

[
  {
   "Fieldid": "Fieldid/112",
    "fieldName": "TX.Sessionval.cost",
    "stringval": "jklah-dw-4c8d-8320-das313s3ASsda|000725N8WuUrfwAS7alP|banker_name"
  }
]

My current postman test is limited to field id only and saving it to variable.

postman.setEnvironmentVariable("Stdid", jsonData.newFields[112].stringValue) 
GPs
  • 67
  • 1
  • 10
  • what will cahnge ? – PDHide Feb 22 '21 at 18:14
  • I don't understand your question. What should be the result for the situation from your question? – pavelsaman Feb 22 '21 at 18:15
  • Fieldid": "Fieldid/112 is only value changes, and the bankern_name in stringval (as mentioned) fieldid would be /118 or /129 /234 .. so on – GPs Feb 22 '21 at 18:16
  • jsonData.newFields[112].stringvalue , so whats the issue with this lcoator? – PDHide Feb 22 '21 at 18:17
  • @PDHide @ pavelsaman : no issue with locater, it works well. but my question was related to located gets changes every time from newFields[112], newFields[128] so on.. how to save string value only based on fieldName which remains constant in whole JSON – GPs Feb 22 '21 at 18:20
  • see the added answer it does that – PDHide Feb 22 '21 at 18:23

1 Answers1

1
pm.environment.set("stdid",jsonData.newFields.filter((a)=>a.fieldName==="TX.Sessionval.cost")[0].stringval)

use filter method of javascrpt array

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • thanks, what does [0] signifies, let me try this and update – GPs Feb 22 '21 at 18:25
  • 1
    the filter returns an array , as the "fieldName": "TX.Sessionval.cost", is unique it will return an array of 1 element , we are getting taht element back as [0] – PDHide Feb 22 '21 at 18:27
  • it works for filtering thanks, how to save that string value only into variable – GPs Feb 22 '21 at 18:33
  • pm.environment.set("stdid" saves it to variable you can use the code as it is , this is the new way to store envirnment variables – PDHide Feb 22 '21 at 18:35
  • when print "stdid" it shows ` "Fieldid": "Fieldid/112", "fieldName": "TX.Sessionval.cost", "stringval": "jklah-dw-4c8d-8320-das313s3ASsda|000725N8WuUrfwAS7alP|banker_name" ` Looking to save the stringval only – GPs Feb 22 '21 at 18:38
  • "stringval": "jklah-dw-4c8d-8320-das313s3ASsda|000725N8WuUrfwAS7alP|banker_name" – GPs Feb 22 '21 at 18:40
  • lol i thought it was more complex... clean and easy, any other way around ? – GPs Feb 22 '21 at 18:44
  • 1
    this is the cleanest option – PDHide Feb 22 '21 at 18:45
  • was my question okay in 1st place ? or do i need to update – GPs Feb 22 '21 at 18:46
  • could you upvote also , you can edit question to make this infomation like the arry index was changing and all – PDHide Feb 22 '21 at 18:47
  • any idea if this has to be filter the same in groovy script? – GPs Feb 23 '21 at 18:18