What is the best way to transform a text file into an array of objects?

I have a text that resembles the following structure:

{"age": "52", "id": 1, "name": "Hulk"}
{"age": "33", "id": 2, "name": "Iron Man"}

My goal is to parse the file and transform it into an array of objects.

This is the progress I have made so far:

const fs = require("fs");
const customerFile = fs.readFileSync("./customers.txt", "utf-8");
const customerArr = customerFile.split("\n");

Although I successfully split the file into an array, I am struggling with converting the array elements into objects. Can anyone provide guidance on how to achieve this?

Answer №1

The data format you are dealing with is known as ndjson. Consider searching for a parser designed specifically for this format.

If you are reading the data line by line into an array, you can then convert it into objects using JSON.parse.

customerArr.map(item => JSON.parse(item));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for retrieving all input fields within a form that are of the types text, password, file, textarea, and selectbox?

Is there a way to retrieve all input fields of specific types within a form using jQuery? jQuery("#signup-form").find('input').each(function(index, element) { }); I am aware that I can target text boxes with: jQuery("#signup-form").find(&apos ...

Using localStorage in the client side of nextJS is a breeze

Encountering an error while attempting to retrieve local storage data. Even with the use client directive in place at the beginning, the issue persists. 'use client'; const baseURL = 'https://localhost:7264'; const accessToken = localSt ...

I'm unsure of the most efficient way to condense this statement

$(document).ready(function(){ if ($(window).width() <961){ $('.item').on('click',function(){ /*---do something---*/ }) }else{ $('.item').on('click',function(){ ...

How can I utilize the parseFloat feature within my dedicated parseFloat function located in an angular factory?

What is the solution for accessing parseFloat within an angular factory function named parseFloat? angular .module('myApp') .factory('mathService', [MathService]); function MathService() { return { parseFloat: myGl ...

Is it possible to use CSS to create a gap between the cursor and the placeholder text within an input field?

Could you please assist me with a bug I have encountered on an older version of Firefox for OSX (37.0.2)? I have included a screenshot of the issue here: https://i.sstatic.net/dzK0G.png Is there a way to use css to move the first character of the placehol ...

Issue encountered when attempting to add multiple JavaScript functions through Greasemonkey using unsafeWindow

One issue that arises is that the functions are unable to recognize each other. For example: unsafeWindow.helloworld = function() { alert('Hello world!'); helloworld2(); // this fails with an error indicating it does not exist } unsa ...

Angular's change detection mechanism triggers too frequently

I'm dealing with a situation in one of my controllers where I have the following code: @HostListener('window:resize') onResize() { this.currentWindowWidth = window.innerWidth; } This code determines different views to render based on the ...

Ways to disable mousewheel function in jQuery

I am trying to deactivate the mouse scroll in jQuery and successfully did so, however, I encountered this error message jquery.min.js:2 [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See Thi ...

The Heroku system encountered an issue: ENOENT - file or directory not found, trying to access '.env'

I'm encountering issues while attempting to deploy my application to Heroku: Error: ENOENT: no such file or directory, open '.env' 2019-04-10T01:38:23.050188+00:00 app[web.1]: 1 at Object.openSync (fs.js:438:3) 2019-04-10T01:38:23 ...

I am struggling to successfully read and parse a CSV file into an array

Link to my CSV file: https://drive.google.com/file/d/0B-Z58iD3By5wb2R2TnV0Rjc3Zzg/view After trying multiple references, I am still unable to properly separate the csv file by using "," as the delimiter. Is there a solution that can help me get an array s ...

Retrieve a specific record from a JSON string using MySQL

My current approach involves storing data as a JSON string in MySQL due to the array of advantages it provides for my specific situation. These JSON strings can often become lengthy and contain multiple entries with unique IDs. As of now, I retrieve the e ...

Deep-diff JavaScript functions are not permissible for use

In my AngularJS application, I am currently attempting to utilize a JavaScript package. To reference it in my index.html file, I added the following code: <script src="deep-diff-0.3.1.min.js"></script> Furthermore, in my controller, I am usin ...

What steps are needed to craft a customized greeting on discord using the latest nodejs update?

I am facing an issue where the following code is not sending anything: client.on("guildMemberAdd", (member) => { const welcomeMessage = "Please give a warm welcome to <@${member.id}> <a:pepesimp:881812231208181790> as th ...

Tips for adjusting the size of an HTML canvas to fit the screen while preserving the aspect ratio and ensuring the canvas remains fully visible

I'm working on a game project using HTML canvas and javascript. The canvas I am using has dimensions of 1280 x 720 px with a 16:9 aspect ratio. My goal is to have the game displayed at fullscreen, but with black bars shown if the screen ratio is not 1 ...

Filtering data in Angular based on specific dates

Upon receiving data from an Angular service, I have a JSON object structured like this: [ { "id": 2, "order_status": "R", "order_date": "2015-09-12T07:58:24.733834Z", "update_timestamp": "2015-10-05T04:22:44.904227Z" ...

Utilizing jQuery and AJAX, execute a PHP query based on the user's input and showcase the query's outcome without reloading the page

For the past 24 hours, I've been on a quest to find a solution. Although I've come across similar inquiries, either the responses are too complex for my specific scenario (resulting in confusion) or they are of poor quality. The crux of my issue ...

Ways to display JSON objects containing nested key-value pairs

In my JSON structure, each Mealplan includes a key and a corresponding value which is an object called meal. id: 1, mealsPerWeek: { Monday: { id: 4, name: "Burger", }, Tuesday: { id: 3, name: "Salad&qu ...

What are the steps to rectify the issue of displaying data accurately on

I am working on my ReactJS project and using devextreme to create a unique circular gauge. However, I'm facing an issue where certain values are not being displayed correctly. For instance, instead of showing the value 5011480286.78, it is displaying ...

Tips for updating the border color of a button:

Struggling to alter the border color of a button Attempting borderColor attribute proved futile: borderColor: '#FFFFFF' Anticipated result Code snippet: headerBtn: { backgroundColor: 'black', fontSize: '16px', f ...

What is the best approach to modify a user-inserted value on a webpage with both javascript and php?

In my PHP code, I have set up a variable that is displayed in an input field for the user. The user has the ability to change this value, however, when I try to retrieve the updated value using JavaScript, it doesn't seem to update. Instead, it remain ...