0

I have json file containing multiple objects in a single file I want to convert them to JSON array how to do this javascript. My overall goal is generate a CSV file out of it.

Here is the sample file content,

{
  Name:"nom1",
  Cities:['city1','city2']
}
{
  Name:"nom2",
  Cities:['city4','city5']
}

Note above is the data dump it is not correct format yet , I want to convert it to following format

var data = [ { Name:"nom1", Cities:['city1','city2'] }, { Name:"nom2", Cities:['city3','city4'] } ]}; 

Then pass it to the below script.

I have thousands of such objects in a single file. I want to generate CSV data as follows

|Name|Cities|

|nom1|city1 |

|nom1|city2 |

|nom2|city3 |

|nom2|city4 |

I am using node javascript to achieve this ,

const { Parser, transforms: { unwind } } = require('json2csv');

const data = {
  Name:"nom1",
  Cities:['city1','city2']
}
const fields = ['Name'];
const transforms = [unwind({ paths: ['features'] })];

const json2csvParser = new Parser({ fields, transforms });
const csv = json2csvParser.parse(myCars);

console.log(csv);

9
  • Where did you get that "example file"? Because without variables assignment, it's not even meaningful JS: those objects will immediately get thrown away. Commented May 15, 2020 at 17:44
  • That is raw data dump I have , I need to convert them into an array of objects and pass it into my script Commented May 15, 2020 at 17:50
  • var data = [ { Name:"nom1", Cities:['city1','city2'] }, { Name:"nom2", Cities:['city3','city4'] } ]}; Commented May 15, 2020 at 17:52
  • 1
    Then please update your post, don't put "new" or "updated" code in comments. Right now, the first block of code you're showing isn't really anything: no tool would dump to plain JS format (because it's not JSON yet), but even if they did, they wouldn't be dumping the code you're showing because you can't do anything with that data. So: what tools are you using? Commented May 15, 2020 at 18:02
  • 1
    A quick and dirty hack would be to read the file line-by-line and replace the (regex) pattern ^[}]$ (lines with nothing but a closing brace) with }, ( skip the replacement for the last line). Assemble the lines to a single string, enclose it with square brackets, and call JSON.stringify(...). Commented May 15, 2020 at 18:02

1 Answer 1

1

This would do the trick with the file exactly like the one you presented:

log-parser.js

const fs = require('fs')

async function fileToArray (file) {
  const fileContent = await fs.promises.readFile(file)
  const singleLined = fileContent
                .toString()
                .replace(/\n/g, '')
                .replace(/\}\{/g, '},{')
                .replace(/(\w+):/g, '"$1":')
                .replace(/'/g, '"')

  console.log(singleLined)
  const array = JSON.parse(`[${singleLined}]`)
  console.log(array)
}

fileToArray(process.argv[2])
node log-parser.js my-log-file.log
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Mestre San, When I pass { Name:"nom1", Cities:['city1','city2'] } { Name:"nom2", Cities:['city4','city5'] }
I get } "Cities":['city4','city5']
Hi @karansys, I've created a gist with all the steps for you to reproduce the solution gist.github.com/danielsan/235959b92c7b6425dc6d94f154a3c654

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.