Convert your .json array to a .jsonl (JSON Lines)

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, y…


This content originally appeared on DEV Community and was authored by Ian Macartney

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 .json*l*

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}


This content originally appeared on DEV Community and was authored by Ian Macartney


Print Share Comment Cite Upload Translate Updates
APA

Ian Macartney | Sciencx (2024-07-24T00:05:57+00:00) Convert your .json array to a .jsonl (JSON Lines). Retrieved from https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/

MLA
" » Convert your .json array to a .jsonl (JSON Lines)." Ian Macartney | Sciencx - Wednesday July 24, 2024, https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/
HARVARD
Ian Macartney | Sciencx Wednesday July 24, 2024 » Convert your .json array to a .jsonl (JSON Lines)., viewed ,<https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/>
VANCOUVER
Ian Macartney | Sciencx - » Convert your .json array to a .jsonl (JSON Lines). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/
CHICAGO
" » Convert your .json array to a .jsonl (JSON Lines)." Ian Macartney | Sciencx - Accessed . https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/
IEEE
" » Convert your .json array to a .jsonl (JSON Lines)." Ian Macartney | Sciencx [Online]. Available: https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/. [Accessed: ]
rf:citation
» Convert your .json array to a .jsonl (JSON Lines) | Ian Macartney | Sciencx | https://www.scien.cx/2024/07/24/convert-your-json-array-to-a-jsonl-json-lines/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.