-1

I´ve read similar question regarding this and I can´t find a solution. I am converting a CSV xml file to a json array of objects. The following function does it succesfully:

const csvtojsonV2 = require("csvtojson");

csvtojsonV2().fromFile('./CSV_TO_JSON/file.xml')
    .then((jsonObj) => {

        console.log(jsonObj)

    })

But now I´d like to get that jsonObj and put it in a file inside a folder in my current workspace:

const uuidv1 = require('uuid/v1')
const fs = require('fs')
const path = require('path')

csvtojsonV2().fromFile('./CSV_TO_JSON/file.xml')
    .then((jsonObj) => {

        var folderNameJSON = uuidv1()
        folderNameJSON = 'JSON_CONVERTED'

        fs.mkdirSync(folderNameJSON)
        fs.writeFileSync(path.join(__dirname, folderNameJSON, 'file.json'), jsonObj)


    })

However when I open the file it just appears: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],etc...

¿Is this how it supposed to be seen? How can I make it appear as an array of objects like it appears when I console.log(jsonObj) in the first code snippet?

J. Cake
  • 331
  • 4
  • 19

2 Answers2

3

Before save to file convert json to string with JSON.stringify https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/JSON/stringify

David
  • 519
  • 2
  • 11
1

To write in a file you have to convert your object to string.

Just use

let objString = JSON.stringify(jsonObj);
fs.writeFileSync(path.join(__dirname, folderNameJSON, 'file.json'), objString);
Maxime
  • 26
  • 4