-1

As you can see based on the columns from the excel one column has "space" which is the Categorized Options that when being parsed the 'Categorized Options': was mixed to the Options key as you can see on the json data below. Categorized Options is the only column key that has space. The Categorized Options should be a seperate key. Is there a way we can alter the paparse json result below to seperate the Categorized Options: key and value to get the desired result ?

Desired Output - The ouput i wanna get

 [{ Type: 'New',   
  Stock: 'P10092',
  VIN: '1G1YY34U455128500',    
  Year: '2005',
  Options:
   ' Switch',
  Categorized Options:
  'LICENSE PLATE BRACKET',
   ImageList:
   'http: image.com',
  Comment: '2005 CHEVY CORVETT',
  FuelType: 'Gasoline Fuel',
  DriveType: 'RWD' }]

Excel columns

Type | VIN | Stock | Year | Options | Categorized Options | ImageList | Comment | FuelType | DriveType
New    | 1G..  | P10092   |  2005   |  Switch   | LICENSE PLATE BRACKET | http: image.com 2005  | comment test  | Gasoline Fuel | RWD

Using Paparse (The data result is now )

this is the result after paparse the 'Categorized Options': was being joined to the Options , the output i want is the data above i posted. Categorized Options should be a seperate key

[{ Type: 'New',   
  Stock: 'P10092',
  VIN: '1G1YY34U455128500',    
  Year: '2005',
  Options:
   ' Switch',
   'Categorized Options':
   'LICENSE PLATE BRACKET,',
   ImageList:
   'http: image.com',
  Comment: '2005 CHEVY CORVETT',
  FuelType: 'Gasoline Fuel',
  DriveType: 'RWD' }
  • Where is your code? – Phil Sep 06 '19 at 03:42
  • The code is no longer needed I already have provided the result –  Sep 06 '19 at 03:45
  • I think you can play with it using JS based on the The data result I have posted –  Sep 06 '19 at 03:45
  • Looks like you're out of luck with that parser given [it does not currently support regular-expression delimiters](https://github.com/mholt/PapaParse/issues/649). Change your CSV to use a more standardised format without the whitespace padding – Phil Sep 06 '19 at 03:57

1 Answers1

1

Make sure to set the delimiter to |. You can also use transform and transformHeader to trim the values.

More informations on PapaParse docs.

const dataText = document.getElementById('data').textContent.trim();
const parsed = Papa.parse(dataText, {
  header: true,
  delimiter: '|',
  transformHeader: header => header.trim(),
  transform: value => value.trim()
});

document.querySelector('#output').innerHTML = JSON.stringify(parsed, null, 2);
<script src="https://www.papaparse.com/resources/js/papaparse.js"></script>

<h3>Data</h3>
<pre id="data">
Type | VIN | Stock | Year | Options | Categorized Options | ImageList | Comment | FuelType | DriveType
New    | 1G..  | P10092   |  2005   |  Switch   | LICENSE PLATE BRACKET | http: image.com 2005  | comment test  | Gasoline Fuel | RWD
</pre>

<h3>Output</h3>

<pre id="output">
</pre>
Teh
  • 2,767
  • 2
  • 15
  • 16