Querying offline exported Sanity datasets with GROQ
Did you know you can query your .ndjson
files from Sanity dataset exports with GROQ? Here's how I did it, and how you can try too.
Did you know you can query your .ndjson
files from Sanity dataset exports with GROQ? Check this out:
I wanted to explore the idea of letting users query backup data in the Baccup application, and started by exploring what the ndjson
format really was. Turns out, itâs really simple. Itâs just a giant JSON array, except objects in that array are separated by a newline. ndjson
stands for newline-delimited JSON.
That got me thinking, it would be trivial to do a sort of âobject visualizerâ, where each object (which would have an arbitrary schema) could be represented visually and queried. I was probably thinking about my experience working with MongoDB at that point. I could then possibly create a GraphQL-like query language to query these objects and create some sort of filter function to explore the dataset.
If you got here in the previous paragraph, youâre smarter than me (low bar). After I made that connection, I looked up GROQ and turns out, thereâs a specification document. TLDR, I learned that itâs a query language to query any JSON document (read: not just Sanity ones) and best of all, just when I thought I wanted to work on my own GROQ implementation in TypeScript, I discovered groq-js
already exists.
(I told you the bar was low. )
From here, itâs just about creating an interface where you could select any .ndjson
file and run the groq-js
library against it. You can try it out on your own .ndjson on this page.
Or if you want to try running GROQ queries against any JSON object input, check out groq.dev.
- GROQ works with any JSON input, not just Sanity dataset exports. This gave me the idea of creating a minimalist CMS that runs on just a
.json
file, and where the query language is still GROQ. - With an assets-included Sanity dataset export, asset entries are included simiar to this:
image@file://./images/379904115c36fd84f7b1e996188b5bccc7e9662f-150x150.jpg
. This maps neatly to the assets folder in the same export. - Looking at
.ndjson
files gave me the idea of portable websites that could run completely offline with a sort of lightweight GROQ client, but keeping Sanity as the CMS/content-editing experience. - we could plausibly create entire âpoint-in-timeâ website snapshots with a little engineering.
Sanity has released the GROQ spec and tools to work with it, so it was really easy to connect the dots and put all this together. I can only imagine the powerful tools we could create for the Sanity community. Definitely excited to experiment more in this area.
Sanity â build remarkable experiences at scale
Sanity Composable Content Cloud is the headless CMS that gives you (and your team) a content backend to drive websites and applications with modern tooling. It offers a real-time editing environment for content creators thatâs easy to configure but designed to be customized with JavaScript and React when needed. With the hosted document store, you query content freely and easily integrate with any framework or data source to distribute and enrich content.
Sanity scales from weekend projects to enterprise needs and is used by companies like Puma, AT&T, Burger King, Tata, and Figma.