Skip to content Skip to sidebar Skip to footer

Nodejs Convert Text To JSON

For some reason I'm having such a hard time converting this txt file to an actual javascript array. myJson.txt {'action': 'key press', 'timestamp': 1523783621, 'user': 'neovim'} {'

Solution 1:

Since your file contains a JSON object per line, you could read that file line by line, using readline.

Each line is then parsed, and push into an array, which is then returned (resolved) after the file is fully read.

'use strict';

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

function convert(file) {

    return new Promise((resolve, reject) => {

        const stream = fs.createReadStream(file);
        // Handle stream error (IE: file not found)
        stream.on('error', reject);

        const reader = readline.createInterface({
            input: stream
        });

        const array = [];

        reader.on('line', line => {
            array.push(JSON.parse(line));
        });

        reader.on('close', () => resolve(array));
    });
}


convert('myJson.txt')
    .then(res => {
        console.log(res);
    })
    .catch(err => console.error(err));

Solution 2:

I would have done this in this way

var fs = require('fs');
var readline = require('readline');
var array = [];
var input = null;
var rd = readline.createInterface({
    input: fs.createReadStream(__dirname+'/demo.txt')
    
});

rd.on('line', function(line) {
    array.push(JSON.parse(line));
});
rd.on('close', function(d){
  array.forEach(e=>console.log(e.action))
})

What's happening here is, I am reading the lines of the file on by one using readline which is one of the core modules of nodejs. Listening on the events and doing what needed.

And yeah, you'll have to parse the line to JSON for sure ;)

Thanks


Solution 3:

The problem with your code is that you're trying to parse JS array as JSON array. while JSON array string should be only string.

Here what you're trying to do:

jsArray = ['{"foo": "bar"}, {"foo":"baz"}']

This is a valid JS array of a single string value '{"foo": "bar"}, {"foo":"baz"}'.

while

jsonArrayStr = '["{"foo": "bar"}, {"foo":"baz"}"]' 

This is a valid JSON array string (as the square brackets is part of the string).

So as to get your code running, you need to add the square brackets to your string before parsing it.

function convert(input_file_path) {
    const file = fs.readFileSync(input_file_path, 'utf8');
    const newFormat = file
      .replace("{", "[{")
      .replace(/}$/, "}]")

    console.log(JSON.parse('[' + newFormat + ']'));
}

Solution 4:

What I'm doing in the script is reading the content of text file line by line and storing it to array along with converting it to JSON object. When we reach last line and our JSON array/object has all the data. Now you can write this object to a new file fs.writeFileSync() after converting JSON object to string with JSON.stringify().

Note :- You've to install Line reader package i.e. npm install line-reader

var lineReader = require('line-reader');
var fs = require('fs')

var jsonObj = {};
var obj = [];
var file = "fileName.json"
var num= 0;

lineRead();
async function lineRead(){
lineReader.eachLine('input.txt', function(line, last) {
// to check on which line we're.
      console.log(num);
      num++;
      convertJson(line)

      if(last){
//when it's last line we convert json obj to string and save it to new file.
        var data = JSON.stringify(obj)
        fs.writeFileSync(file,data);

      }
});
}

function convertJson(data){
        var currentVal = data
        var value = JSON.parse(data)
        var temp = value;
//storing the value in json object
        jsonObj = value;
        obj.push(jsonObj);
    }
  
}

Post a Comment for "Nodejs Convert Text To JSON"