Help with Nested Arrays - Json

David Bovill david at viral.academy
Tue Apr 19 12:55:35 EDT 2016


You want something like this:

*repeat* for each line somePath in filePaths
>
*put* somePath into someKey
>
*split* someKey with tab
>
*put* anythingYouWant into pathArray [someKey]
>
*end* *repeat*


Note the trick is to put anything (can be empty) into the array in the last
line. Then you array is not going to get clobbered in the next loop.


On 19 April 2016 at 00:03, Sannyasin Brahmanathaswami <brahma at hindu.org>
wrote:

> Disclaimer: I don’t know what I’m doing
>
> I want to, as they say, in the UK, “come to grips” with nested arrays and
> then, secondarily, importing and exporting to and from JSON.
>
> I have an actual use case in front of me. I could turn this over to one of
> the brainiacs on our team (Andre, David) but then I won’t learn anything.
>
> So I come again with a begging bowl, asking in simple terms and then
> perhaps newbies lurking will also gain from this.
>
> OK: I need to take a functional specification/screens mocked up on
> Indesign and turn it into a “map” that describes the cards and elements on
> the cards. These cards comprise a hero image at the top, and a series of
> small groups below which act as links/buttons. These links in turn form a
> big group that can be scrolled and used to trigger navigation to other
> cards/modules in the app.
>
> I start simply by using Excel, filling in a table, export to tab-delimited
> data, the attempting to parse that data into a nested multi-level array.
>
> This is a snippet, where the first column is the card, the second column
> is the group type, then we have the “params” related to each of the
> link/button/groups (label, background image path, system message type,
> target and orientation of the destination card.
>
> home    hero-image      Portals to Saivite Hinduism
>  img/portal-heros/home-hero.jpg
> home    link    Gems of\nWisdom
> img/portal-link-thumbs/home/home-gems_thm.jpg   goModuleStack
>  gems-module     portrait
> home    link    Surprise\nMe
> img/portal-link-thumbs/home/home-surprise_thm.jpg       goPortalCard
> surprise-portal portrait
> home    link    Look    img/portal-link-thumbs/home/home-look_thm.jpg
>  goPortalCard    look-portal     portrait
> home    link    Listen  img/portal-link-thumbs/home/home-listen_thm.jpg
> goPortalCard    listen-portal   portrait
> home    link    Read    img/portal-link-thumbs/home/home-read_thm.jpg
>  goPortalCard    read-portal     portrait
> home    link    Practice
> img/portal-link-thumbs/home/home-practice_thm.jpg       goPortalCard
> practice-portal portrait
> home    link    Our\nWebsite
> img/portal-link-thumbs/home/home-website_thm.jpg        goPortalCard
> website-portal  portrait
> home    link    Fun \n Games
> img/portal-link-thumbs/home/home-fun_thm.jpg    goPortalCard    fun-portal
>     portrait
> surprise-portal hero-image      Surprise Me
>  img/portal-heros/surprise-hero.jpg
> surprise-portal link    Verses from\nthe Vedas
> img/portal-link-thumbs/surprise-portal/vedas-verses_thm.jpg
>  fetchTextReader http://www.himalayanacademy.com/sivasiva-api/random-vedas
>      portrait
> surprise-portal link    Tirukural\nCouplets
>  img/portal-link-thumbs/surprise-portal/tirukural-verses_thm.jpg
> fetchTextReader http://www.himalayanacademy.com/sivasiva-api/random-kural
>      portrait
> surprise-portla link    Inspired\nAudio Talks
>  img/portal-link-thumbs/surprise-portal/talks_thm.jpg    playAudio
> http://www.himalayanacademy.com/sivasiva-api/random-talk        portrait
> surprise-portal link    Songs\nAnd Chants
>  img/portal-link-thumbs/surprise-portal/songs_thm.jpg    playAudio
> http://www.himalayanacademy.com/sivasiva-api/random-some        portrait
> surprise-portal link    Sacred\nArt
>  img/portal-link-thumbs/surprise-portal/art_thm.jpg      displayImage
> http://www.himalayanacademy.com/sivasiva-api/random-art portrait
> surprise-portal link    Gurudeva's\nDarshan
>  img/portal-link-thumbs/surprise-portal/gurudeva_thm.jpg displayImage
> http://www.himalayanacademy.com/sivasiva-api/random-gurudeva    portrait
> surprise-portal link    Bodhinatha's\nDarshan
>  img/portal-link-thumbs/surprise-portal/bodhinatha_thm.jpg
>  displayImage
> http://www.himalayanacademy.com/sivasiva-api/random-bodhinatha  portrait
> surprise-portal link    Video\nWorld
> img/portal-link-thumbs/surprise-portal/video_thm.jpg    playVideo
> http://www.himalayanacademy.com/sivasiva-api/random-video       landscape
> surprise-portal link    Five Inspired Quotes
> img/portal-link-thumbs/surprise-portal/trilogy-quotes_thm.jpg
>  fetchTextReader
> http://www.himalayanacademy.com/sivasiva-api/random-trilogy     portrait
> read-portal     hero-image      Read, Learn, Study
> img/portal-heros/read-hero.jpg
> read-portal     link    Frequently\nAsked\nQuestions
> img/portal-link-thumbs/surprise-portal/faq_thm.jpg      goModuleStack
>  faq-module      portrait
> read-portal     link    Gurudeva's\nSpiritual\nToolbox
> img/portal-link-thumbs/surprise-portal/gr-toolbox_thm.jpg
>  goModuleStack   gr-toolbox-module       portrait
> read-portal     link    All Our\nOn-Line\nBooks
> img/portal-link-thumbs/surprise-portal/publications_thm.jpg     browseWeb
>      http://www.himalayanacademy.com/readlearn/books landscape
> read-portal     link    Lesson of\nthe Day
> img/portal-link-thumbs/surprise-portal/daily-lesson_thm.jpg     browseWeb
>
> http://www.himalayanacademy.com/livespiritually/become-student/todays-lesson
>   portrait
> read-portal     link    Path to Siva:\nA Catechism\nfor Youth
>  img/portal-link-thumbs/surprise-portal/path-to-siva_thm.jpg
>  goModuleStack   path-to-siva-module     portrait
> read-portal     link    A Character\nBuilding\nWorkbook
> img/portal-link-thumbs/surprise-portal/character-building_thm.jpg
>  goModuleStack   character-building-module       portrait
> read-portal     link    Educational\nInsights
>  img/portal-link-thumbs/surprise-portal/edu-insights_thm.jpg     browseWeb
>      http://www.himalayanacademy.com/site/search/file_id/_ei portrait
> read-portal     link    Hindu\nTimeline
> img/portal-link-thumbs/surprise-portal/timeline_thm.jpg browseWeb
> http://www.himalayanacademy.com/book/dancing-with-siva/527     portrait
>
>
> Now I want to load this into a nested array and run JsonExport.  I go this
> far:
>
> local tGroupCounter, aPortalLinks
> on mouseUp
>    # clear the array
>    put empty into aPortalLinks
>    answer "Use Data Input path?" with "No" or "Yes"
>    if it is "no" then
>       answer file "Choose your tab delimited file" with "OK"
>       put it into fld "dataInput"
>    end if
>    put url ("file:" & fld "dataInput") into tData
>    set the itemdel to tab
>
>    put "SivaSiva" into aPortalLinks["tStack"]
>    put "0" into tGroupCounter
>
>    repeat for each line x in tData
>       put item 1 of x into aPortalLinks["stack"]["tCard"]
>       put item 2 of x into tGroupType
>       if tGroupType = "link" then
>          add 1 to tGroupCounter
>          put (tGroupType & tGroupCounter) into tGroupType
>       end if
>       put tGroupType into aPortalLinks["tStack"]["tCard"]["tGroup"]
>       put item 3 of x into
> aPortalLinks["tStack"]["tCard"]["tGroup"]["label"]
>       put item 4 of x into
> aPortalLinks["tStack"]["tCard"]["tGroup"]["imgPath"]
>       put item 5 of x into
> aPortalLinks["tStack"]["tCard"]["tGroup"]["message"]
>       put item 6 of x into
> aPortalLinks["tStack"]["tCard"]["tGroup"]["target"]
>       put item 7 of x into
> aPortalLinks["tStack"]["tCard"]["tGroup"]["orientation"]
>    end repeat
>    put JsonExport(aPortalLinks)
> end mouseUp
>
> If we exit the repeat loop entirely immediately  I get this output
>
> {"tStack": "SivaSiva”}
>
> If I let it run…  I get this output which is from the very last line in
> the data:
>
> {
>         "stack": {
>         "tCard": "read-portal"
>         },
>         "tStack": {
>                 “       tCard": {
>         "tGroup": {
>                 "imgPath":
> "img/portal-link-thumbs/surprise-portal/timeline_thm.jpg",
>                 "message": "browseWeb",
>                 "label": "Hindu\\nTimeline",
>                 "target": "
> http://www.himalayanacademy.com/book/dancing-with-siva/527",
>                 "orientation": "portrait"
>                 }
>                 }
>         }
> }
>
> Instead of building up the array… somehow my method is wiping out that
> “higher dimensions” of the array.
> Leaving us with a single element instead of elements for each line of the
> inputData
>
> This has nothing to do with the JsonExport library.. I can see this
> happening in the variable watcher before we ever get to the JsonExport
> function. Setting a break point at the top of the script and stepping in
> the debugger, the variable Watcher shows:
>
> aPortalLinks
>    tStack   —>   SivaSiva  # so far so good...
>
>
> Then stepping to next line
>
> aPortalLinks
>    tStack >          # now empty? Why?
>      tCard > home
>
> Then stepping to next line
>
> aPortalLinks
>    tStack >
>       tCard >       # now empty? Why?
>          tGroup > link1
>
> Then stepping to next line
>
> aPortalLinks
>         tStack >
>                 tCard >
>                         tGroup > # now empty? Why?
>                                 imgPath >
> img/portal-link-thumbs/surprise-portal/timeline_thm.jpg
>                                 Message >browseWeb
>                                 Label > Hindu\\nTimeline
>                                 Target >
> http://www.himalayanacademy.com/book/dancing-with-siva/527
>                                 Orientation > portrait
>
>
> And in the end… only the last line is output to Json, with the last
> iteration of params for the group intact.
>
> What am I doing wrong?
>
> BR (8.0.0 RC1)
>
> Secondary issue: we need to represent “CR” kin the input data… I used the
> \n  as prescribed by JSON rules… … but the JsonExport is escaping the
> backslash
>
> input:   firstLine\nSecondLine
> output:  firstLine\\nSecondline
>
> Anyway to avoid this? Because if we need to run a JsonIMport later… I
> think \\n will break it and not be interpreted correction as a new line -
> CR. (TBD)
>
>
> BR
>
>
>
>
>
>
>
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode



More information about the use-livecode mailing list