Extracting JSON Arrays from inside Json Objects

Sannyasin Brahmanathaswami brahma at hindu.org
Fri Jul 15 14:28:08 EDT 2016


We do have control over the formation of the JSON, which is done by our web server API.

for every media item record in the "item" table, there is a corresponding series of records in the "item_formats" table. So the JSON is returning the values for all the records in the item_formats table that are related to the one record in the item table.  one media item will have many item_format entries

So the item_format column "filename" has the file name(s) for the media item on disk;  "format_name" is a generic column that carries DCIM and W3C standard metadata about the that file (audio: mp3, books epub mobi, pdf; art: 300x500, 1000x1400 etc…)

I actually think I like using a nested object, because then I don't have to wrap my head around thinking of it differently as a "string/list"  We use a lot of deeply nested multi-dimensional arrays on the web server (revIgniter) so our brains are wired already for this style ["formats"][1]["filename"]

keeps it all consistent too… no subtle nuanced change of methods needed in the parser code app side…

BR

mark wrote:

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


More information about the use-livecode mailing list