Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
HI, I am starting with Thingworx. I have a query as I am getting service response as jsonarray and i want to validate one parameter of each object in Jsonarray. how to do that using validator. i tried to create one parameter in validator and using expression i am validating it but validator is called for first object of array and it validates the first object only but how to validate the remaining items in Array? Appriciate help in advance.
Thanks.
Hi @DeepakParihar Look at the below reference post, and let us know if you have any doubts.
its works for first object of json array. but how to get all response of service and validation each object using validator. i have seen loops are not supported in validator expressions.
So If i get it correctly, you want to validate the JSON array inside the validator? You might use a loop and test if either all of the objects are valid or one of them is invalid and so on.
If you want to validate individual object and return valid/invalid for each object in runtime, that might not be possible, as the length might be dynamic, unless you have a static array of objects.
Loops are not allowed in validator expression.
If you pass the JSON in the Validator you can loop through it.
I am using a simple hardcoded Array in this example, but I think a JSON object should work the same way.
let arr = [1, null, 3];
let hasInvalidElements = (testArr) => testArr.some((element) => element === null);
Output = hasInvalidElements(arr);
In this case I am using the "some" Method to check if there are "null" values inside the array. This method will stop iterating as soon as the provided function (element === null) returns true for an element. So no further unnecessary iteration.