Jump to content

How to retrive JSON array length?


Moore Tone
 Share

You are about to reply to a thread that has been inactive for 3857 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

My apologies if the question has already been addressed before, but I have not been able to find any discussion about it.

How can I get the number of elements of JSON Array?
Something more or less similar to llGetListLength () but for a JSON Array.

Thanks in advance for any helpful answer.

Link to comment
Share on other sites

use llJson2List ....?

 

 state_entry()
    {   purch = llList2Json( JSON_ARRAY, [] );
        info = llList2Json (JSON_OBJECT, [    
       "Purchases", purch ] );
        Customers = llList2Json( JSON_OBJECT, [] );
    }

  touch_end( integer num)
    {  
          name = llDetectedName(0);
          
          if (llJsonGetValue (Customers, [name] ) == JSON_INVALID)
          {   Customers = llJsonSetValue (Customers, [name], info); }

    }

 ----------- >

 list purchased = llJson2List (llJsonGetValue (Customers, [name, "Purchases"]));

integer length = llGetListLength (purchased)

Link to comment
Share on other sites

An alternative to Xiija's (correct) answer is:

 

integer length = -1;while (llJsonGetValue(Customers, [name, "Purchases", ++length]) != JSON_INVALID);// length is now equal to the number of items within the array

 

Her solution has the advantage of speed, having fewer function calls, while this approach has a memory advantage, in that the array in question isn't expanded out into a list which may be a consideration with dealing with largish arrays. It works by taking advantage of the fact that if one attempts to access a nonexistent element within a JSON text, you'll get JSON_INVALID as a return value.

Taking that a step further, if what you're wanting to do is process each element in the array one at a time (what's known as iterating over an array), you neither need the length of the array nor do you need to expand it into a list:

 

string element;integer length = -1;while ((element = llJsonGetValue(Customers, [name, "Purchases", ++length])) != JSON_INVALID){	// process element here}

 

There's also a third approach that one can use if the size of an array is to be frequently needed and you wish to avoid the computational costs of the previous two- storing the size of the array within the array itself. This is done by using the first position (index 0) and incrementing the number there with each addition to the array (or, once we get JSON_DELETE, decrementing the number each time an element is removed) and otherwise treating the array as index 1 based instead of index 0 based. This adds a bit more computation for every addition (or deletion) and an extra element within the array but makes getting the size of it simply a matter of:

 

integer length = llJsonGetValue(Customers, [name, "Purchases", 0]);

 

[ETA: This is the structure  of the JSON text Xiija and I are working with in our examples (commented integers illustrate the 3rd example, where the length of the array is being stored within the array):

Customers{	"customerA": {		"Purchases": [			// 3,			"item1",			"item2",			"item3"		]	},	"customerB": {		"Purchases": [			// 2,			"item1",			"item2"		]	},	"customerC": {		"Purchases": [			// 4,			"item1",			"item2",			"item3",			"item4"		]	}}

 

 

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3857 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...