How to read a file and convert to json array in javascript?

Kerwen

If I have a file like below:

key1 key2
data1 data2
data3 data4
...

Is there an easy way to read this file and convert it to a json object array? The final output I want is :

[{key1:data1, key2: data2},{key1: data3, key2: data4}, ...]
Alexander Nenashev
import fs from 'fs';

// read the file and split into rows and cells
const data = fs.readFileSync('./text.txt').toString().split('\n').map(row => row.match(/\w+/g));
// get the first row with cells as key names
const head = data.shift();

// map the rest of the rows into objects with keys from the first row
const result = data.map(row => Object.fromEntries(row.map((cell, idx) => [head[idx], cell])));
console.log(JSON.stringify(result));

The result:

/usr/local/bin/node ./read-csv.mjs
[{"key1":"data1","key2":"data2"},{"key1":"data3","key2":"data4"}]

If you need some speed boost with a big file, use Array::reduce():

import fs from 'fs';

const data = fs.readFileSync('./text.txt').toString().split('\n').map(row => row.match(/\w+/g));
const head = data.shift();

const result = data.map(row => row.reduce((obj, cell, idx) => {
    obj[head[idx]] = cell;
    return obj;
}, {}));
console.log(JSON.stringify(result));

If you need the maximum speed use for loops:

import fs from 'fs';

const data = fs.readFileSync('./text.txt').toString().split('\n').map(row => row.match(/\w+/g));
const head = data.shift();

const result = [];

for (let i = 0; i < data.length; i++) {
    const row = data[i];
    const item = result[result.length] = {};
    for (let j = 0; j < row.length; j++) {
        item[head[j]] = row[j];
    }
}

console.log(JSON.stringify(result));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript: How to convert an JSON arrays in array into an CSV file?

How to convert JSON to Array in Javascript

How to read Json file and convert it to dataframe

Read JSON file to a Javascript Array without JQuery

Read file and convert to JSON

how to convert json array to javascript array

How to convert an array of JSON into an array of javascript object?

How to convert JSON array to OBJECT array javascript?

How to read a json file in this format of array in swift?

How to read the content of a .txt file as an array with javascript?

How to read a text file into an array of objects in JavaScript

Vanilla Javascript, how to read local JSON file

How to read an external local JSON file in JavaScript?

How to read an external JSON file as a string in javascript?

How to read json file and convert to case class with Spark and Spray Json

How to convert a form to a Json array in javascript

How to convert JSON object to JavaScript array?

How to Convert nested json to array of object in Javascript?

How to convert dynamic JSON string to javascript array

How to convert json to array object in javascript?

How to convert JSON Object into Javascript array

how to convert this JavaScript object that contains an array to JSON?

how to convert json to array in javascript for an li list

how to convert array api response to json in JavaScript

How to convert mySQL JSON column into javascript array?

how to convert a text file into a json object in javascript?

How can I read local json file with array data using javascript?

Read txt file and convert to array

Read in a JSON Array and convert to a IEnumerable