How to use JSONL in NodeJS

Dhaval Dhrangadhariya
2 min readApr 7, 2023

--

JSON and JSONL are both data interchange formats used to represent structured data in a human-readable and machine-readable form. JSON is a format that stores data in a single file, while JSONL is a format that stores data in multiple lines in a text file, with each line containing a single JSON object.

JSON Lines vs JSON

  • JSON is typically used for representing a single object, while JSONL is used for representing multiple objects, each on its own line.
  • JSON is a single-file format, while JSONL is typically used for storing data in multiple lines in a single file.
  • JSONL is useful for streaming large amounts of data, while JSON is not designed for streaming and is typically read into memory all at once.
  • JSONL is useful for storing and processing large amounts of data because it allows data to be streamed line by line, rather than reading the entire file into memory.

Sample JSONL vs JSON

JSONL will look like below

{"id":"1","name":"John Doe","languages_known":["English"]}
{"id":"2","name":"Michel Jordan","languages_known":["English","French"]}
{"id":"3","name":"Dwayne Johnson","languages_known":["French"]}

JSON will look like below

[
{"id":"1","name":"John Doe","languages_known":["English"]},
{"id":"2","name":"Michel Jordan","languages_known":["English","French"]},
{"id":"3","name":"Dwayne Johnson","languages_known":["French"]}
]

NodeJS sample code

Here is a sample code to read the jsonl file in Nodejs

const readline = require('readline');
const fs = require('fs');

// create a readline interface for reading the file line by line
const rl = readline.createInterface({
input: fs.createReadStream('path/to/file.jsonl'),
crlfDelay: Infinity
});

// create an array to hold the parsed JSON objects
const jsonArray = [];

// read each line of the file and parse it as JSON
rl.on('line', (line) => {
jsonArray.push(JSON.parse(line));
});

// log the parsed JSON objects once the file has been fully read
rl.on('close', () => {
console.log(jsonArray);
});

Conclusion

In conclusion, both JSON and JSONL are useful formats for representing structured data, but the choice of format depends on the specific needs of your application.

If you have enjoyed this article and would like to buy me a coffee ☕️follow this dhavalsoni9989/BMC.

--

--

No responses yet