Running a query from the CLI and outputting it as a .ndjson file in Sanity.io

3 replies
Last updated: Feb 14, 2024
Hi, is it possible from the cli to run a query, and output it as a
.ndjson
file? Not just json.
pnpm -F studio exec sanity documents query --dataset archive '*[_type == "event"]{..., "tags": [], "fromArchive": true }[0..1]' > ./apps/studio/exports/archived-events.ndjson <<<<<
My use case is that I want to query some documents from an "archive" dataset, and then import then into another dataset.
But the dataset import only takes ndjson, while the query only outputs json.
Feb 14, 2024, 9:43 AM
I would like to do it all with cli, so I don't have to run scripts.
Feb 14, 2024, 9:45 AM
Found this great one-liner:
cat test.json | jq -c '.[]' > testNDJSON.json

Feb 14, 2024, 9:48 AM
Yep, that’s what I use as well.
Feb 14, 2024, 4:58 PM

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?