Impemented algorithm for formatting JSON reponses to REST queries