1

I have a simple txt file with data in this format with millions of lines:

{"a":9876312,"b":1572568981512}
{"a":9876312,"b":1572568981542}

I want to convert this into a file with "dot" json extension file using reduce function in NodeJs and return statement, probably looking like this:

[{"a":9876312,"b":1572568981512},
{"a":9876312,"b":1572568981542}]

Any help will be really really appreciated. Thanks :)

SO far I tried this:

const fs = require('fs');
const FILE_NAME = 'abc.txt';
const x = mapEvents(getJSONFileData(FILE_NAME));

function getJSONFileData(filename) {
   return fs.readFileSync(filename, 'utf-8')
   .split('\n')
   .map(JSON.parse)
}

function mapEvents(events) {
events.reduce((acc, data) => {
  return [{data.a, data.b}]
});
}

console.log(x)

I am getting an 'undefined' value constantly

thealchemist
  • 397
  • 7
  • 24

1 Answers1

1

I have found some issues, in your code.

  1. You haven't returned anything from mapEvents function, that makes your varaible x value undefined.
  2. getJSONFileData needs some fixing.

You can use below code:-

const fs = require('fs');
const FILE_NAME = 'abc.txt';
const x = mapEvents(getJSONFileData(FILE_NAME));

function getJSONFileData(filename) {
  return fs
    .readFileSync(filename, 'utf-8')
    .split('\n')
    .filter(Boolean)
    .map(JSON.parse);
}

function mapEvents(events) {
  return JSON.stringify(events);
}

console.log(x);
  • When implementing your code i noticed that, If i have a number like '4611687012137402260' in the code.. it is getting turned into '4611687012137402000'.. because it is being treated as a number.. any way to overcome that? – thealchemist Dec 23 '19 at 02:55
  • @thealchemist this issue is related to https://stackoverflow.com/questions/15869275/json-not-converting-long-numbers-appropriately – Sakul Budhathoki Dec 23 '19 at 04:31
  • 1
    const fs = require("fs"); const FILE_NAME = "abc.txt"; const x = mapEvents(getJSONFileData(FILE_NAME)); function getJSONFileData(filename) { return fs .readFileSync(filename, "utf-8") .split("\n") .filter(Boolean); } function mapEvents(events) { return JSON.stringify(events).replace(/\\|"/g, ""); } console.log(x); – Sakul Budhathoki Dec 23 '19 at 04:32
  • @thealchemist you can use above code to fix this issue. – Sakul Budhathoki Dec 23 '19 at 04:32
  • That actually converts the entire json array into one string.. its not a valid json anymore.. :/ – thealchemist Dec 23 '19 at 07:56
  • const fs = require("fs"); const FILE_NAME = "abc.txt"; const x = mapEvents(getJSONFileData(FILE_NAME)); function getJSONFileData(filename) { return fs .readFileSync(filename, "utf-8") .split("\n") .filter(Boolean); } function mapEvents(events) { return events.map(item => JSON.parse( item .replace(/":/g, `":"`) .replace(/,"/g, `","`) .replace(/}/, `"}`) ) ); } console.log(x); – Sakul Budhathoki Dec 23 '19 at 08:07
  • @thealchemist try this, it will convert number to string from the array item. – Sakul Budhathoki Dec 23 '19 at 08:08