Extracting JSON Arrays from inside Json Objects

Mark Wieder mwieder at ahsoftware.net
Fri Jul 15 13:46:44 EDT 2016


Sannyasin Brahmanathaswami <brahma at ...> writes:

> Thanks Peter.. I get it… I think… In the following we actually have a
nested object. Hence the need to use [1].

Exactly. Although the [1] is not because it's a nested object, but because
it's (in this case) a collection of one nested JSON object.

You have several options to deal with this, depending on how much control
you have over the system.

You can stick with what you posted, and then for the case where you have
multiple formats you would use the numeric order, as in [1]
This will work, but I find it awkward to remember the order,
1 = mp3
2 = pdf
etc.

If you have control over the generation of the JSON object, then you could
either generate a collection

formats = {key:value, key:value, etc}

but given the fact that the key is named in the plural I'm guessing that's not
what you want. In that case you could do

formats = { "mp3": { "file": "filePath", etc},
  "pdf" : {"file": "filePath", etc}
}

and so on. If you don't have control over the JSON generation aspect of the
system and are just consuming JSON objects that are generated externally
then stick with the [1] format (and use constants).

-- 
 Mark Wieder
 ahsoftware at gmail.com


More information about the use-livecode mailing list