Count results from JSON request

This forum contains topics that were moved from Get Satisfaction
Chris2656882
Posts: 0
Joined: Fri May 11, 2012 3:47 pm

Count results from JSON request

Post by Chris2656882 »

How can one count the number of results from a JSON request before it is rendered into a list or grid?

Also, how can one append to a list or grid rather than overwriting it with new data?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Count results from JSON request

Post by maxkatz »

On service's success event run this JavaScript:

code
var count = data.results.length;
/code

'results' is the actual array name, the variable in your JSON object might be different.

As for appending, it has to be implemented by the developer. One option is to save all data into local storage, combine, and then run a Generic Service to do the mapping: https://getsatisfaction.com/tiggzi/to...

Sean Kelley
Posts: 0
Joined: Thu Oct 11, 2012 2:25 pm

Count results from JSON request

Post by Sean Kelley »

Max-I am trying to count my collection results as you describe:
I run this on service success:

//the following line does not get count and creates exception of data is not defined / cannot read property length of undefined.
//var count = data.results.length;

//this logs count correctly but creates exception that data is not defined.
var count = data.length;
console.log(count);
localStorage.setItem('total', count);

I am confused about how I could be getting the correct count and the error at the same time. How to I access the length correctly?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Count results from JSON request

Post by Kateryna Grynko »

Hi Sean,

The variable "data" points to data got from Rest Service.
In Max's example array was stored in response parameter "result". If Rest service returns an array then your code for array length definition is correct:
codevar count = data.length;/code

The error "data is not defined" may be caused by JS code in another Action. Do you have an Action on "Complete" event?

Sean Kelley
Posts: 0
Joined: Thu Oct 11, 2012 2:25 pm

Count results from JSON request

Post by Sean Kelley »

the array makes sense. With regard to the error on data, I did have a complete action but deleted it. I now only have one action under Data on success.

When I watch the console, the exception occurs after the successful count is logged. I thought that I might have lest a test bit of code somewhere else but cannot find it. both the count result and the error occur in the same js file

Uncaught ReferenceError: data is not defined startScreen.js:299
$.die.live.click startScreen.js:299
jQuery.event.dispatch jquery-1.8.2.js:3063
elemData.handle.eventHandle

Sean Kelley
Posts: 0
Joined: Thu Oct 11, 2012 2:25 pm

Count results from JSON request

Post by Sean Kelley »

thanks- the problem appeared to be with execution order. I invoked service on button click, then page transitioned, but on data side I ran script to get count on service success. By moving page transition to data side after script ran, error went away. thanks.

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

Count results from JSON request

Post by EJLD »

hello! I am looking for a similar line of code on Success.
my count query works when tab-test the service.
however, may I ask you for the proper line of code to forward that count result to a localStorage or a Appery label ?
thk you in advance,
Eric

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Count results from JSON request

Post by Alena Prykhodko »

Hello EJLD,

Custom code is outside the scope of our support, please show us what you have tried and what doesn't work.

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

Count results from JSON request

Post by EJLD »

thks for your prompt feedback.
I managed to find the way to map the count.
best

RobertJay
Posts: 0
Joined: Fri Jun 15, 2012 1:32 pm

Count results from JSON request

Post by RobertJay »

This was fantastically helpful to me just now - not only for the count, but for getting the value of a specific record index (e.g. var t1 = data[3].lastname). Thank you Kateryna.

Post Reply