JSON.stringify and toString() method, as well as string() all result in doubled quotation marks

116 views Asked by At

I simply do a dict assignment operation like this headers[col] = JSON.stringify(value);

when parsing a TSV and it always results in the JSON keys with double quotation marks when I both use JSON.stringify and toString() method, as well as string().

E.g. the JSON I get outputted:

  {
    '"Beruf"': 'Vorarbeiter',
    '"Anrede"': 'Herr',
    '"Nachname"': 'Emil',
    '"Vorname"': 'Gurke',
    '"Eintritt"': 34407,
    '"Geb.Tag"': 24004,
    '"STR"': 'Max-Planck-Str. 1',
    '"Ort"': 'Marzahn',
    '"Postleitzahl"': 2232145,
    '"Mobil"': '+4942314151512351351513',
    '"Mail"': '[email protected]',
    '"Personalnummer"': 1067
  }

What is the way to have just the singular quotation marks once like so '' and what am I doing wrong with this assignment?

1

There are 1 answers

1
Heiko Theißen On

The superfluous quotation marks come from the column headers in the tab-separated values format:

"Beruf"     "Anrede" ...
Vorarbeiter Herr     ...

You don't want these quotation marks in the member names of your Javascript object, therefore strip them away:

headers[col.replace(/^"(.*)"$/, "$1")] = value;

(I see no need to use JSON.stringify(value) either, because that will again introduce quotation marks.)