Node.js Express JSON Online Converter to CSV,XLSX,XML,CSON,YAML and JSON5 Using any-json Library Full Tutorial 2020

Node.js Express JSON Online Converter to CSV,XLSX,XML,CSON,YAML and JSON5 Using any-json Library Full Tutorial 2020

 

 

Welcome folks today in this tutorial we will be talking about how to convert JSON Files to CSV,XLSX,XML,YAML and CSON . So this will be the online JSON Converter using any-json library. All the source code of the application is shown below.

 

Live Demo

 

Online JSON to CSV Converter

Online JSON to XML Converter

Online JSON to Excel Converter

Online CSV to JSON Converter

Online Excel to JSON Converter

Online XML to JSON Converter

 

 

Get Started

 

 

In order to get started we need to install the following dependencies

 

npm init -y

 

npm i express any-json ejs nodemon

 

Now after installing all these dependencies we can start our node.js project

 

So for converting it we need to install this dependency any-json globally on our machine by adding a -g flag

 

npm i -g any-json

 

So after it you can execute the command to carry out the conversion process like this

 

any-json convert package.json output.csv

 

This command will convert package.json file to output.csv file just simple command

 

any-json convert input.csv package.json

 

This command will convert the csv file to json file

 

Now make a index.js file and copy paste the following code

 

index.js

 

 

const express = require('express')

const app = express()

const multer = require('multer')

const {exec} = require('child_process')

app.set("view engine","ejs")

const bodyParser = require('body-parser')

app.use(bodyParser.json())

app.use(bodyParser.urlencoded({extended:false}))

app.use(express.static("public"));

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "public/uploads");
  },
  filename: function (req, file, cb) {
    cb(
      null,
      file.fieldname + "-" + Date.now() + path.extname(file.originalname)
    );
  },
});

const jsonFilter = function (req, file, callback) {
  var ext = path.extname(file.originalname);
  if (
   ext !==. ".json"
  ) {
    return callback("This Extension is not supported");
  }
  callback(null, true);
};

var  jsonfileupload = multer({
  storage: storage,
  fileFilter: jsonFilter
});


app.get('/',(req,res) => {

res.render('jsonconverter')

})

app.get('/csvtojson',(req,res) => {

res.render('csvtojson',{title:'FREE CSV File to JSON Online Converter Tool - Online CSV File to JSON File Converter Tool - FreeMediaTools.com'})

})

const csvFilter = function (req, file, callback) {
  var ext = path.extname(file.originalname);
  if (
    ext !== ".csv"
  ) {
    return callback("This Extension is not supported");
  }
  callback(null, true);
};

var csvtojsonupload = multer({
  storage: storage,
  fileFilter: csvFilter
});

app.post('/csvtojson',csvtojsonupload.single('file'),(req,res) => {

var outputpath = Date.now() + "output.json"

if(req.file){

exec(`any-json convert ${req.file.path} ${outputpath}`,(err,stdout,stderr) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
res.send(err)
}

res.download(outputpath,(err) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
res.send("Unable to download the file")
}

fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)

})

})

}


})




app.get('/xmltojson',(req,res) => {

res.render('xmltojson',{title:'FREE XML File to JSON Online Converter Tool - Online XML File to JSON File Converter Tool - FreeMediaTools.com'})

})

const xmlFilter = function (req, file, callback) {
  var ext = path.extname(file.originalname);
  if (
    ext !== ".xml"
  ) {
    return callback("This Extension is not supported");
  }
  callback(null, true);
};

var xmltojsonupload = multer({
  storage: storage,
  fileFilter: xmlFilter
});

app.post('/xmltojson',xmltojsonupload.single('file'),(req,res) => {

var outputpath = Date.now() + "output.json"

if(req.file){

exec(`any-json convert ${req.file.path} ${outputpath}`,(err,stdout,stderr) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
res.send(err)
}

res.download(outputpath,(err) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
res.send("Unable to download the file")
}

fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)

})

})

}


})






app.get('/jsontocsv',(req,res) => {

res.render('jsontocsv',{title:'FREE JSON File to CSV Online Converter Tool - Online JSON File to CSV File Converter Tool - FreeMediaTools.com'})

})

const jsonFilter = function (req, file, callback) {
  var ext = path.extname(file.originalname);
  if (
    ext !== ".json"
  ) {
    return callback("This Extension is not supported");
  }
  callback(null, true);
};

var jsonupload = multer({
  storage: storage,
  fileFilter: jsonFilter
});

app.post('/jsontocsv',jsonupload.single('file'),(req,res) => {

var outputpath = Date.now() + "output.csv"
var inputfile = Date.now() + "input.json"

if(req.file){

var jsonarray = []

jsonarray.push(JSON.parse(fs.readFileSync(req.file.path)))

console.log(fs.readFileSync(req.file.path))

console.log(jsonarray)

fs.writeFileSync(inputfile,JSON.stringify(jsonarray))

exec(`any-json convert ${inputfile} ${outputpath}`,(err,stdout,stderr) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)
res.send(err)
}

res.download(outputpath,(err) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)
res.send("Unable to download the file")
}

fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)

})

})

}


})






app.get('/jsontoxml',(req,res) => {

res.render('jsontoxml',{title:'FREE JSON File to XML Online Converter Tool - Online JSON File to XML File Converter Tool - FreeMediaTools.com'})

})


var jsonupload = multer({
  storage: storage,
  fileFilter: jsonFilter
});

app.post('/jsontoxml',jsonupload.single('file'),(req,res) => {

var outputpath = Date.now() + "output.xml"
var inputfile = Date.now() + "input.json"

if(req.file){

var jsonarray = []

jsonarray.push(JSON.parse(fs.readFileSync(req.file.path)))

console.log(fs.readFileSync(req.file.path))

console.log(jsonarray)

fs.writeFileSync(inputfile,JSON.stringify(jsonarray))

exec(`any-json convert ${inputfile} ${outputpath}`,(err,stdout,stderr) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)
res.send(err)
}

res.download(outputpath,(err) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
fs.unlinkSync(inputfile)
res.send("Unable to download the file")
 }

fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
fs.unlinkSync(inputfile)

})

})

}


})








app.get('/jsonfiletoexcel',(req,res) => {

res.render('jsonfiletoexcel',{title:'FREE JSON File to Excel Online Converter Tool - Online JSON File to XLS/XLSX File Converter Tool - FreeMediaTools.com'})

})




var jsonupload = multer({
  storage: storage,
  fileFilter: jsonFilter
});

app.post('/jsonfiletoexcel',jsonupload.single('file'),(req,res) => {

var outputpath = Date.now() + "output.xlsx"
var inputfile = Date.now() + "input.json"

if(req.file){

var jsonarray = []

jsonarray.push(JSON.parse(fs.readFileSync(req.file.path)))

console.log(fs.readFileSync(req.file.path))

console.log(jsonarray)

fs.writeFileSync(inputfile,JSON.stringify(jsonarray))

exec(`any-json convert ${inputfile} ${outputpath}`,(err,stdout,stderr) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)
res.send(err)
}

res.download(outputpath,(err) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)
res.send("Unable to download the file")
}

fs.unlinkSync(req.file.path)
fs.unlinkSync(inputfile)
fs.unlinkSync(outputpath)

})

})

}


})


app.get('/exceltojson',(req,res) => {

res.render('exceltojson',{title:'FREE Excel File to JSON Online Converter Tool - Online XLS/XLSX File to JSON File Converter Tool - FreeMediaTools.com'})

})


const exceltojsonFilter = function (req, file, callback) {
  var ext = path.extname(file.originalname);
  if (
    ext !== ".xls" && ext !== ".xlsx"
  ) {
    return callback("This Extension is not supported");
  }
  callback(null, true);
};


var exceltojsonlatestupload = multer({
  storage: storage,
  fileFilter: exceltojsonFilter
});

app.post('/exceltojson',exceltojsonlatestupload.single('file'),(req,res) => {

var outputpath = Date.now() + "output.json"

if(req.file){

var jsonarray = []


exec(`any-json convert ${req.file.path} ${outputpath}`,(err,stdout,stderr) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
res.send(err)
}

res.download(outputpath,(err) => {

if(err){
fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)
res.send("Unable to download the file")
}

fs.unlinkSync(req.file.path)
fs.unlinkSync(outputpath)

})

})
}
})

app.listen(5000)

 

READ  Build a Presendential Alert Meme Generator in Browser Using HTML5 CSS3 and Javascript Full Project For Beginners

 

jsonfiletoexcel.ejs

 

 

<div class="container">
          <h1 class="text-center">
              JSON File to Excel
          </h1>
          <form action="/jsonfiletoexcel" method="post" enctype="multipart/form-data">
            <div class="form-group">
        <label for="json">JSON File:</label>
                <input class="form-control" type="file" name="file" id="" required>
            </div>
            <div class="form-group">
                <button class="btn btn-block btn-danger">
                    Download Excel File
                </button>
            </div>
        </form>
        <br><br>

 

READ  Node.js Express Stripe API Payment Gateway Integration to Setup Online Payments Full Tutorial 2020

 

 

jsontocsv.ejs

 

 

<div class="container">
          <h1 class="text-center">
              JSON to CSV Online
          </h1>
          <form action="/jsontocsv" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="json">JSON File:</label>
                <input class="form-control" type="file" accept=".json" name="file" id="" required>
            </div>
            <div class="form-group">
                <button class="btn btn-block btn-danger">
                    Download CSV
                </button>
            </div>
        </form>
        <br><br>

 

 

 

Screenshot

 

 

 

 

Leave a Reply