Best answer

Count number of JSON arrays

  • 21 May 2022
  • 3 replies
  • 6921 views

Userlevel 1

I have a configurable list in Jotform that comes to Zapier in what looks like JSON(?):

[{"Name":"Luke Skywalker","Number":"123456","Degree":"Test","Specialty":"Test","Owner/Associate":"Owner","Count":"1"},{"Name":"Buzz Lightyear","Number":"654321","Degree":"Test","Specialty":"Test","Owner/Associate":"Associate","Count":"1"}]

 

I am able to run the following:

output = {};

var obj = JSON.parse(inputData.JOTFORM);

for (var i = 0; i < obj.length; i++) {
   output["Test "+ i] = JSON.stringify(obj[i]);

}

And get the output:

Test 0

{"Name":"Luke Skywalker","Number":"123456","Degree":"Test","Specialty":"Test","Owner/Associate":"Owner","Count":"1"}

Test 1

{"Name":"Buzz Lightyear","Number":"654321","Degree":"Test","Specialty":"Test","Owner/Associate":"Associate","Count":"1"}

 

This does split them up, but I am not sure how to get a count from that (with the above example having 2). I don’t necessarily need to spilt them, I really just want to know how many there are. 

 

Any help would be appreciated. Thanks!

 

icon

Best answer by GetUWired 23 May 2022, 18:38

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +14

Hi @cheeseman 

Good question.

Please check out this help topic:

 

Userlevel 7
Badge +12

Hi @cheeseman 

Since you are dealing with an array already you won’t need to use the split as shown in Troy’s example. You will however use the .length shown. please try the following code

 

output = {};

//parse input into valid json
var obj = JSON.parse(inputData.JOTFORM);

//Split up json array into individual objects
for (var i = 0; i < obj.length; i++) {
output["Test "+ i] = JSON.stringify(obj[i]);
}

//set a new output key equal to the array length;
output.totalCount = obj.length;

 

 

Userlevel 1

Thank you! That worked!