Aaron and Steve are a killer combo for high quality courses like High Performance SQLite. The course shows at great detail the ins and outs of SQLite and how to scale it to compete with any other database. I love it!Nik Spyratos
Build with and integrate SQLite into your production applications.
I've made my test data available for you to use and follow along.
(keyboard keys clacking) We're gonna look at some JSON aggregation functions now. There are two, but there are really four, but they're like kind of the same. So we have JSON group array and JSON group object. And so that is when you have a table and you're grouping by something, you can turn some of those values into a JSON object or a JSON array instead of maxing or summing or you know, concatting. We can turn those into JSON objects. So I said there are two, but there are really four. The other two are just the JSON B versions of the same thing. So you have JSON group array, JSON B group array, JSON group object, JSON B group object. We're gonna start with JSON group array. If I do select * from products, I have five products in here, not terribly interesting. Three fruits, two vegetables. Now, if I were to do select category, and I want to, I wanna group it on category, but I wanna retain the names of all the products, but I don't wanna do a group concat with a comma separated or pipe delimited or anything. I want some actual JSON. So JSON group array. And we're gonna say name, that's it. From products group by category. There you go. Category fruit, category vegetable, and a proper, honest to goodness JSON array with their names in there. This is cool. We can get a little bit fancier and we can say, let's say JSON objects. Ooh, it's all starting to come together. So let's say the key is name and the value is name, and the key is price as a string. And the value is price pulled out of the column. And so if we were to look at that now, that's potentially even more interesting. We've got a full on proper array. And inside each array or each array item rather, is a JSON object that has the name and the price. This is a very cool implementation of JSON group array. So what we're saying is, as SQLlite, as you're grouping this, as you're squishing all of these rows together, every time, why don't you create a new JSON object and here's how you do it. Key name, value name, key price, value price. And put that into this group array that we are running. The next function that we're gonna look at is JSON Group objects. So let's start by saying select * from employees. I got another dinky little table here, not terribly interesting. There are five people in there. So if we did select JSON group object and we put name and salary in there from employees. Now we didn't really group by anything. So the entire table is just being compressed down into one value and you see that the key is the name. So that gives us Alice there and the value is salary. And so that has created this key value structure. So we've basically compressed this table into a JSON object. Let us do something a little bit different. We can say select department, if you can spell department and we'll do this as employee. Yeah, that's fine, employees, from employees group by department. And if we do that, that's a little bit more interesting. So we have, engineering has Alice at 75 and Charlie at 80. Marketing has Diana and sales has Bob and Eve. And so this is similar but different, it creates a JSON object. It's all right there in the name. So if you want to, if you wanna group it down into an array, you've got JSON group array. And if you want to group it down into an object where you control the key and the value, you can do that with JSON Group object. Remember that you have JSON B variance of both. If you're continuing on with your processing or putting this back in the database, we were actually looking at it and in this fictional example, probably handing this back to the application to do some work. And so we went with the plain JSON versions. So this is a very cool way to do a little bit more heavy lifting on the database side. Of course, you could bring all the data back and then iterate over it in your favorite programming language, but databases are really good at data. And so I like to push as much of the data manipulation down to the database as possible and keep all my business logic in the app layer. That's just kind of a preference that I have. Let the database do what it's good at, let the app do what it is good at. And I find that the database handling some of this aggregation, turning it into JSON, it's a pretty good use case for it. So JSON group object, JSON group array.