Page 1 of 1

looping through an array of JSON objects

Posted: Mon Jul 27, 2020 11:20 am
by jackie200_
Hi, I am new to LiveCode and I was trying to find a way to loop through an array of JSON objects.

for example: [{"name":"max", "number":"2222"}, {"name":"lisa", "number":"3333"}]

I tried repeat for each element as well as repeat for each key but I cannot find any other way to loop through the JSON objects besides manually splitting the array by "}," to get individual JSON objects.

Is there a way to loop through the array to get one JSON object example: {"name":"max", "number":"2222"} for every loop without splitting the array string manually?

Thank you in advance. :)

Re: looping through an array of JSON objects

Posted: Mon Jul 27, 2020 1:45 pm
by Klaus
Hi Jackie,

welcome to the forum!

Since I have few knowledge of JSON stuff, I would convert the JSON to a LC array and parse that one:

Code: Select all

...
put JSONTOARRAY(your_json_array_here) into the_LC_Array
## Now parse the LC array.
...
Best

Klaus

Re: looping through an array of JSON objects

Posted: Mon Jul 27, 2020 5:48 pm
by bn
Hi Jackie,

With your example above try the folowing

Make a new stack with 2 fields and 1 button.

Paste your json from above into field 1

set the script of the button to

Code: Select all

on mouseUp
   put field 1 into tData
   put JSONToArray(tData) into tArray
   repeat for each key aKey in tArray
      if tArray[aKey]["name"] = "max"
      then put "Max" & cr & tArray[aKey]["number"] into field 2
      exit repeat -- just bail out because you have found what you were looking for
   end repeat
end mouseUp
There is no easy way to parse json in Livecode, using the function JSONToArray() returns a Livecode array with your data. Then you can access your data via that array.

Kind regards
Bernd

Re: looping through an array of JSON objects

Posted: Mon Jul 27, 2020 11:16 pm
by rkriesel
bn wrote:
Mon Jul 27, 2020 5:48 pm
...

Code: Select all

on mouseUp
   put field 1 into tData
   put JSONToArray(tData) into tArray
   repeat for each key aKey in tArray
      if tArray[aKey]["name"] = "max"
      then put "Max" & cr & tArray[aKey]["number"] into field 2
      exit repeat -- just bail out because you have found what you were looking for
   end repeat
end mouseUp
---
Hi, Bernd and Jackie. You can use "filter" to do that without a repeat loop, as in this example:

Code: Select all

command filterFromJSON pJSON, pProperty, pValue
  get JSONtoArray(pJSON)
  filter keys of it where it[each][pProperty]=pValue
  return it for value
end filterFromJSON
-- Dick

Re: looping through an array of JSON objects

Posted: Tue Jul 28, 2020 12:06 am
by bn
Hi Dick,

thanks for reminding me of "filter" of an array again.

I put your code into a script that Jackie can put into another button in the stack I proposed above

Code: Select all

on mouseUp
   put field 1 into tData
   filterFromJSON tData, "name", "max"
   
   if it contains "error" then 
      put "JSON could not be parsed" into field 2
      exit mouseUp
   end if
   
   -- there could be multiple occurences of "max"
   repeat for each element anElement in it
      put anElement["name"] & cr & anElement["number"] & cr after tResult
   end repeat
   delete char -1 of tResult -- a return
   
   if tResult is empty then
      put "not found" into tResult
   end if
   
   put tResult into field 2
end mouseUp

command filterFromJSON pJSON, pProperty, pValue
   try
      put JSONtoArray(pJSON) into it
   catch  tError -- contains error messages
      -- problems parsing JSON
      get "Error:" && item - 1 of line 1 of tError -- puts this into variable it
      return it for value
   end try
   
   filter keys of it where it[each][pProperty]=pValue
   return it for value
end filterFromJSON
It also adds a little error checking.

Kind regards
Bernd