![Profile image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fts10onj4%2Fproduction%2F077753b63476b77fb111ba06d1bb538517033a54-3500x3500.jpg&w=96&q=75)
Convert your .json array to a .jsonl (JSON Lines)
![Turning json into jsonl with jq -c '.[]' ./mydata.json > mydata.jsonl](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fts10onj4%2Fproduction%2Fe7ae47d52ec3084511d893b8a8851580630aae32-1008x894.png&w=3840&q=75)
JSON Lines is a file format that stores one JSON object per line in a file. This is more scalable than storing it as a JSON array, since most JSON parsers require loading the full stringified array into memory before it can parse it. With JSON Lines, your code can load one line at a time. If you don't hold onto each object (for instance, you do some operation and save it elsewhere), your code can theoretically read an infinitely long file without running out of memory. It also allows you to easily read a stream of data.
To make these files, each object needs to be on its own line. and the file extension is .jsonl
For Convex import, we allow you to specify either format: a .json
or .jsonl
. However, we limit the .json
size to 8MB. So one question that comes up is how can you transform your .json
file into .jsonl
?
Using jq
to convert .json to .jsonl
jq
is a common tool for working with JSON from the commandline.
Here's the one-liner to convert a .json
file into .jsonl
using jq
:
jq -c '.[]' ./mydata.json > mydata.jsonl
Example
Input: ./mydata.json
[
{
"foo": "bar"
},
{
"easy_as": 123
}
]
Output: ./mydata.jsonl
{"foo":"bar"}
{"easy_as":123}