✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

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
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 remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?