I'm new to Matrix, and I'm trying to get my head around it by diving in head-first and attempting a little test project. So far I've been somewhat successful, but I've hit a few roadblocks that I'm hoping you gurus here might be able to give me a steer on.
Scenario
I have an external web application that generates a simple JSON feed consisting of an array of ~20 objects, each two properties ("text" and "href"), and some also have a third optional property called "collection". I need to be able to retrieve this feed, combine it with additional data sources (publishing in the same format), select a sub-set of this aggregated collection, then publish this subset as a formatted HTML widget, or as re-published JSON data.
I want the publishing to use RESTful URLs, for example http://example.com/aggregate should output the HTML widget, whereas http://example.com/aggregate.json should output the JSON version.
My attempt
The first bit was straightfoward, I've got a JSON Data Source asset which is linked to a REST Resource which retrieves the JSON feed from my external application and is configured to extract the contents into a bunch of Data Source Record Sets (DSRS). I've then got a second JSON Data Source which is linked up to a Text File asset containing JSON, to simulate the other feed which is being aggregated.
These two JSON Data Sources are then in turn passed into an Asset Listing which is configured to pick 6 random DSRSs from that collection. This is all working fine, and as a simple test of the HTML widget publishing I have it outputting as a <ul> with each DSRS appearing as an <li>. So far so good.
So to add the RESTful JSON URL I configured an alternative context which matches URLs ending in .json and added a corresponding Web Path to my Asset Listing. I then edited (in this new context) the Page Contents and Default Type Format to output JSON. This almost works, except for a couple of small gotcha's explained below.
Problem one
When outputting in JSON, trailing commas are a problem (e.g. after the last property of an object, or the last item in an array). This means that if I simply create an asset listing with something like -
Page Contents:
{"feed": [ %asset_listing% ]}
Type Format:
{ "text":"%ds__text%", "href":"%ds__href%" },
then I end up with a malformed output like this,
{"feed": [ { … }, { … }, // <-- extra comma ]}
To solve this I tried to use a keyword modifier along the lines of %asset_position^neq:6:,: % (where "6" is the last asset position) but this doesn't seem to work at all, and still outputs a comma every time. I've had it output %asset_position% to verify I'm supplying a value that really exists, but it just doesn't seem to work as I believe it's suppose to.
Problem two
I can't seem to find a way to associate designs with specific contexts. I have a design with a parse file which outputs an application/json Content-type header, but I can't find a way to serve the correct design based on the context. It's odd, because under Settings on the Asset Listing, Matrix lists both of the URLs (the regular URL and the .json URL) separately when I apply a design, but I don't seem to be able to update the design for each URL independently?
Please help!
I've gone over the manuals and forum posts as best I can. It feels like all the bricks are here, but I'm missing the cement to put them together. I may have misunderstood some basic Matrix concepts, so feel free to enlighten me or suggest alternatives. I'd like to keep the implementation as DRY as possible, which is why I haven't just created two Asset Listings with different URLs (for example).
I look forward to hearing what you might have to say and suggest.