Reputation: 350
I'm new to JSON and haven't been able to find an example of what I'm trying to achieve. I'm looping through my responseBody trying to confirm that each returned equipment model contains partial text of "Tra". How do I parse a value for substring?
{
"categories:[],
"equipment":[
{
"model":"580 Trac Vac"
},
{
"model":"4X2 Trail Gaitor"
},
{
"model":"Trac Mass-Ter 40'"
}]
}
//Confirm Model Partial
var jsonData = JSON.parse(responseBody);
var resultCount = jsonData.equipment.length;
for (i=0;i<resultCount;i++){
var modelString = jsonData.equipment[i].model;
tests["Resultset " +i+ " contains \"Tra\""] = modelString.has("Tra");
}
Can I do something similar or should I be using some jq and regex combination?
Upvotes: 1
Views: 6503
Reputation: 350
Here is the solution with Regex for several variations of "Tra"
var jsonData = JSON.parse(responseBody);
var resultCount = jsonData.equipment.length;
for (i=0;i<resultCount;i++){
var modelString = jsonData.equipment[i].model;
var substring = /(?:(?:(?:TRA))|(?:(?:Tra))|(?:(?:tra)(?:.*)))/;
tests["Resultset " +i+ " contains some form of \"Tra\""] =
modelString.has(substring);
}
Upvotes: 1
Reputation: 4131
You can do it with modelString.indexOf("Tra")
//Confirm Model Partial
var jsonData = JSON.parse(responseBody);
var resultCount = jsonData.equipment.length;
for (i=0;i<resultCount;i++){
var modelString = jsonData.equipment[i].model;
if(modelString.indexOf("Tra") > 0)
{
tests["Resultset " +i+ " contains \"Tra\""] = modelString.has("Tra");
}
}
Or you can do it with a regular expression which would just be Tra
Upvotes: 1