GUIDE: How to return JSON data using a tlist sql tag

How to return JSON data using a tlist_sql tag
In order for your page to be rendered as correct JSON data, this piece of code must be the only content in your page. If there is any HTML, CSS or JavaScript code in this file, the browser (or whatever else is receiving this data) won't see it as JSON data.

Let's use this simple database query as an example:

First insert it into a tlist_sql tag:

There aren't any data fields being displayed from this query yet. Let's add some that fit the JSON format.

Notice the surrounding []. This makes the result set an array of JSON objects.

If there was only one result, we would get valid JSON data output on this page. However, each JSON data element (each {} element) must be separated by commas excluding the last element in the array.

The easiest way I've found to work around this limitation to do the following:

First, notice the comma after the close curly brace. This will place a comma after every result. Second, notice the {} after the end tlist_sql. No matter how many JSON elements are created, we'll always add a blank element to end, which doesn't need the comma at the end.

When you receive the JSON data, you'll have an extra element on the end. To remove that extra element in JavaScript:

[[Category:7]]