Processing Multi-dimension Arrays

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
wlaughto
Posts: 19
Joined: Fri Apr 07, 2017 5:06 am

Processing Multi-dimension Arrays

Post by wlaughto » Wed Jun 10, 2020 3:33 am

I have been wrestling with array processing, not quite sure the best way to attack. Simplistically, I am doing the following:-
  • Access external datastore which returns a JSON 'array' with variable number of records
  • Extract from the JSON array the values of the keys I require
  • Do some processing
The JSON data returned is in the attached text file.

Sample code to access and process:-

Code: Select all

put url "https://data.nsw.gov.au/data/api/3/action/datastore_search?q=2103&fields=postcode,lga_name19,notification_date,likely_source_of_infection&resource_id=2776dbb8-f807-4fb2-b1ed-184a6fc2c8aa" into myJSONVar

put JSONToArray(myJSONVar) into tJSONArray

get the keys of tJSONArray["result"]["records"]
split it by return
repeat for each element tIndex in it
   put tJSONArray["result"]["records"][tIndex]["notification_date"] & tab & \
         tJSONArray["result"]["records"][tIndex]["postcode"] & tab & \
         tJSONArray["result"]["records"][tIndex]["likely_source_of_infection"] & tab & \
         tJSONArray["result"]["records"][tIndex]["lga_name19"] & CR after myJSONData
end repeat
   
--comment Do some other processing
My question - is there a more elegant way to process the array to return the data I need other than:-
  • get the keys of tJSONArray["result"]["records"]
and
  • put tJSONArray["result"]["records"][tIndex]["notification_date"]
etc

Or if I wanted to cycle thru the entire JSON array and when I hit the 'records' 'key', extract the 'id's (eg notification_date, postcode etc) text, and the values associated with these.
Attachments
JSONData.zip
(866 Bytes) Downloaded 20 times

AndyP
Posts: 485
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Processing Multi-dimension Arrays

Post by AndyP » Wed Jun 10, 2020 6:38 am

Hi wlaughto,

That is the way I always process JSONToArray data arrays.

I think its pretty elegant and a simple way to drill down to your selected keys and elements, which is easy to read and understand when you come back to your code at a later date.

Just as a helper, I usually add a treeview widget to easily visualize the data, this can be hidden on release.

eg.

Code: Select all

set the arrayData of widget "Tree View" to tJSONArray
Andy Piddock

http://2108.co.uk/tinyide/ - Mini LC IDE alternative

http://2108.co.uk/script-editor-themer/ - Script Editor colour theming

http://livecodeshare.runrev.com/stack/897/LiveCode-Multi-Search - Search across 8 LC resources

wlaughto
Posts: 19
Joined: Fri Apr 07, 2017 5:06 am

Re: Processing Multi-dimension Arrays

Post by wlaughto » Wed Jun 10, 2020 1:32 pm

👍Thank you for you input. Appreciated.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”