'Difficulty using csvtojson to create nested JSON structure from csv

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.



Solution 1:[1]

try this headers configuration:

headers: ['Name', 'Contests[0].Name', 'Contests[0].Candidates[0].Name', 'Contests[0].Candidates[0].Votes', 'Turnout']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 traynor