Search code examples
arraysjsonarraylistpostmanpostman-testcase

Identify string value in jSON based on FieldName only with array index changes every time


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) 

Solution

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

    use filter method of javascrpt array