looping through an array of JSON objects

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jackie200_
Posts: 1
Joined: Mon Jul 27, 2020 11:07 am

looping through an array of JSON objects

Post by jackie200_ » Mon Jul 27, 2020 11:20 am

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. :)

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: looping through an array of JSON objects

Post by Klaus » Mon Jul 27, 2020 1:45 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: looping through an array of JSON objects

Post by bn » Mon Jul 27, 2020 5:48 pm

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

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: looping through an array of JSON objects

Post by rkriesel » Mon Jul 27, 2020 11:16 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: looping through an array of JSON objects

Post by bn » Tue Jul 28, 2020 12:06 am

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

Post Reply