Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate jsonl objects

Given an API that returns a jsonl, how can I manipulate the data that I obtain?

What if the API gives me data like this:

{"plate": "pizza", "quantity": 3}
{"plate": "pasta", "quantity": 2}
  1. In javascript the object retrieved what type will have?
  2. If I want to add a new object, to have a result like:
{"plate": "pizza", "quantity": 3}
{"plate": "pasta", "quantity": 2}
{"plate": "hotdog", "quantity": 7}

How can I do that maintaining the type .jsonl and not creating and array?

Thanks a lot for the help

like image 361
Omar El Malak Avatar asked Oct 20 '25 13:10

Omar El Malak


1 Answers

According to jsonlines.org, each line in a jsonl file is a valid JSON value.

Thus, the approach would seem to be:

  1. split the file into lines.
  2. parse each line separately as JSON.

Something like this:

const lines = data.split(/\n/);
lines.forEach(line => {
  const object = JSON.parse(line);
  // Do something with object.
});
like image 54
Sjlver Avatar answered Oct 23 '25 03:10

Sjlver