0

How to validate the JSON Response fields? After validating the fields I need to check whether the fields are alphabetically sorted or not. How would I do it ?

1 Answer 1

2

The JSON object is unordered set of name/value pairs. There is no guarangee of ordering at all. You need to take json object keys list and sort them. After sorting access to object fields by sorted keys list.

If you mean how to check list (array) of values. You need to implement simple loop through array and check that each element must be less than next element in sorting comparision criteria.

For js language checking array for alpha ordering may be done like this:

var array = ["Apple", "Application", "AppName", "Happy"];

var isSortedAlpha = array.reduce(function(res, current, index, arr) {
  return res && arr[index&&index-1] <= current
}, true);
Sign up to request clarification or add additional context in comments.

2 Comments

Scenario: I am using a search API and my search string is "App" the response should be Apple. Application, AppName etc. Here how would I check the response order? Thank you.
The question need to be answered in context of api specification and ordering criterias. If you are author of api you can return results in array ordered by search criteria. (First is more sutable etc.) Responce may have also "wrAPPer" "hAPPy". You can reorder such api response in alphabetical. If you write test case for testing smart algorithm api you need to introduce some simplifications and assumptions that do not violate the concept of expected data from algorithm in your test case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.