Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Papa parse returns Data array in which everything is converted to string

I am parsing .CSV to Papa.parse with React, i am getting output something like this:

[
  {Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881"},
  {Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
  {Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
  {Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
  {Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
  {Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
  {Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
  {Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
]

The problem is Papa.parse coverts everything to string under double quotation mark!

How can we convert back them to number? (if they were number in their original form! Problem is that some of the Field may not have number type, say location or address or name: they are string typo)

Or Is there any configuration for preventing Papa.parse to do so?

like image 716
Jayraj Avatar asked Aug 31 '26 10:08

Jayraj


1 Answers

Enable the dynamicTyping option to retain numbers:

const csv = "Year,BMW,Toyota,Mercedes,Location\n1929,1896,9547,4881,Germany"
const csvData = Papa.parse(csv, {header:true, dynamicTyping: true}).data

console.log(typeof csvData[0].Year);
console.log(typeof csvData[0].Location);
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.5.0/papaparse.min.js">
</script>
like image 200
RustyDev Avatar answered Sep 02 '26 22:09

RustyDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!