0

There's a feature in the csvtojson package where you can create a nested JSON object from a CSV file permitting you configure the headers correctly. However, I'm having difficulty applying the schema shown in the above link for my CSV file.

My current CSV structure is this:

Table showing structure of data

And the JSON structure I'm trying to create should look something like this:

{
    Name: 'Bedminster Twp East',
    Contests: [
        {
            Name: 'JUSTICE OF THE SUPREME COURT',
            Candidates: [
                {
                    Name: 'Maria McLaughlin',
                    Votes: 511
                },
                {
                    Name: 'Kevin Brobson',
                    Votes: 857
                },
                {
                    Name: 'Write-In',
                    Votes: 1
                },
            ]
        },
        {
            Name: 'JUDGE OF THE SUPERIOR COURT',
            Candidates: [
                {
                    Name: 'Timika Lane',
                    Votes: 494
                },
                {
                    Name: 'Megan Sullivan',
                    Votes: 870
                },
                {
                    Name: 'Write-In',
                    Votes: 1
                },
            ]
        }
        ...
    ],
    Turnout: '45.77%'
},
...

This is the existing code I have:

const csv = require('csvtojson');
const axios = require('axios');

axios({
    method: 'get',
    url: 'URL'
}).then(function(response) {
    let x = response.data.split('\n');
    x.splice(0, 2);
    let newX = x.join('\n');
    csv({
        noheader: false,
        headers: ['Precinct.Name', 'Precinct.Contests.Name', 'Precinct.Contests.Name.Candidates.Name', 'Precinct.Contests.Name.Candidates.Votes', 'Precinct.Turnout']
    }).fromString(newX).then((csvRow) => {
        console.log(csvRow);
    });
});

Which is unfortunately creating the following JSON structure:

[
  {
    Precinct: {
      Name: 'Bedminster Twp East',
      Contests: [Object],
      Turnout: '47.89%'
    }
  },
  {
    Precinct: {
      Name: 'Bedminster Twp East',
      Contests: [Object],
      Turnout: '47.89%'
    }
  },
  {
    Precinct: {
      Name: 'Bedminster Twp East',
      Contests: [Object],
      Turnout: '47.89%'
    }
  },
  {
    Precinct: {
      Name: 'Bedminster Twp East',
      Contests: [Object],
      Turnout: '47.89%'
    }
  },
... 19841 more items

I don't know exactly what is in the "Contests" object, but considering that the number of objects is equal to the number of rows in the existing CSV file, I believe there is something wrong with my header grouping. I can't tell if I'm severely misreading the documentation linked earlier or if it's not clear enough. Thanks for any assistance.

EDIT: I'm splitting and joining because the first two rows are metadata that says "UNOFFICIAL RESULTS" and "2022", which I don't want being read in.

sxmrxzxr
  • 41
  • 1
  • 8

1 Answers1

0

try this headers configuration:

headers: ['Name', 'Contests[0].Name', 'Contests[0].Candidates[0].Name', 'Contests[0].Candidates[0].Votes', 'Turnout']
traynor
  • 5,490
  • 3
  • 13
  • 23
  • Unfortunately this didn't work, but I appreciate the response. – sxmrxzxr May 17 '22 at 20:14
  • @sxmrxzxr what do you mean, it gave some other structure or.. check the final string, why are you splitting and joining it.. try without that. also, try hardcoding the input data manually – traynor May 17 '22 at 20:21
  • I'll add this to the OP but I'm splitting and joining because the first two rows are metadata that says "UNOFFICIAL RESULTS" and "2022", which I don't want being read in. Your suggestion for the headers gave `{ Name: 'Bedminster Twp East', Contests: [ [Object] ], Turnout: '47.89%' },`, but it still ended up with around 19k rows. – sxmrxzxr May 17 '22 at 20:42
  • @sxmrxzxr well, the structure seems to be the one you wanted. well, check the csv, how many rows there is/should be.. – traynor May 17 '22 at 21:01