I highly recommend High-Performance SQLite to fellow engineers. My motivation for learning SQLite stemmed from my interest in local-first applications, and I needed to understand SQLite to effectively use it in the browser. This resource provided that knowledge in a clear and practical manner, making it a must-watch for modern developers.Yuya Fujimoto
Build with and integrate SQLite into your production applications.
I've made my test data available for you to use and follow along.
(computer keys clacking) Now, I don't want you to get your hopes up. SQLite has no JSON column. Remember the types that SQLite does have and how they are, in fact very, very minimal. This does not change that. They do have support for JSON, but not a JSON column. You need to store your JSON in a text or a blob column. Remember, you can store anything, anywhere, all the time in SQLite. But I recommend a text or blob column for your JSON. So what is the difference between this JSON and JSON-B, business? Well, let's talk about, let's talk about JavaScript. In JavaScripts, typically, JSON comes in over the wire, right? So you're getting JSON from a server or from an API or even pulling it out of local storage, perhaps. And typically what you have to do is you take it out or you get it from somebody and then you say, JSON.parse, right? So you take a string and you turn it into this in-memory object. And then to go the other way, you take the object and you say JSON.stingify, and you turn it back into a string. That is the mental model that we're working with here when we talk about JSON and JSON-B. So all of the JSON functions operate on text. And when you run a JSON function on a string of JSON, it has to convert it into that binary representation. It has to do that JSON.parse under the hood. SQLite is written in C, so it's not really JSON.parse, but you get the idea. So JSON functions operate on JSON strings, strings that represent JSON. They turn it into binary, do the thing, and then turn it back into a string. JSON-B on the other hand, is that binary representation. It is not a string, it is the binary representation. So you get to skip the entire process where SQLite has to look at the string, turn it into JSON, do the operation, turn it back into a string. So typically JSON-B is going to be a lot faster because it skips that whole parsing and then restringifying. Let me show you two of these functions real quick. And then through the rest of this module, we're gonna be talking about JSON versus JSON-B. But I'm just trying to paint a picture for you of the differences of these formats. I'm just gonna write a little bit of SQL here. I'm gonna say JSON extract, which we will go over. And then what I want to extract from is this JSON object. It's just a JSON object where the top level key is foo and it contains an object itself with a key of bar and the value of 42. And I'm gonna use this little magic here to say, extract the foo key and we'll see what we get back. We get back bar 42. Don't worry about the specifics. But what I wanna show you now is if we say JSON-B extract, we don't get back bar 42, we get back this, this nonsense, frankly. And that's because the JSON-B functions return the binary representation of that JSON object. This is SQLite's internal representation. You should not try to figure it out. You should not try to decode it. You should not try to write it. It is internal, it is subject to change. This is the way that SQLite actually sees JSON-B objects such that it does not have to re-parse them. So in this select JSON extract, what happened here was this string was parsed into JSON. Then this path was looked up, and then the result was turned back into text, which is nice and easy for us to read. Here the same thing happened. This string was turned into a binary representation. This key was looked up, but then importantly, it wasn't turned back into text. It was just given to us as a binary representation. Now for our purposes here, this is useless. We can't do anything with that. However, if we were continuing on to operate on this JSON in SQLite, having it as binary already would make it a lot faster. If we were writing this back to disc, if we were writing this back to SQLite it would be great to store it as binary. So as we continue through this module, we'll look at the JSON versus JSON-B functions. But remember, all the JSON-B functions return binary. All the JSON functions return text. They will actually both work with either type of input, but what it returns is very, very different. One returns text, one returns binary. If you're trying to consume it in your application, you probably want the text version. If you're trying to continue operating or write it back into SQLite, you probably want the binary version 'cause that's gonna be a lot faster.