-1

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"': 'test@test.de',
    '"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?

apingaway
  • 33
  • 1
  • 1
  • 17
  • 1
    Don't encode *each value". Just serialise the entire object at once `JSON.stringify(obj)`. – VLAZ Oct 31 '22 at 14:31
  • I tried this, it results in the same behaviour. – apingaway Oct 31 '22 at 14:31
  • Then we'll need a [mcve]. `JSON.stringify()` should provide valid JSON. (And what you are showing here is not.) – Ivar Oct 31 '22 at 14:32
  • Technical note: Javascript has no `dict`s, that's python. You're trying to assign a value to an object property. But more importantly, "when parsing a TSV and it always results in [...]" then please remember to [show that in your post](/help/how-to-ask). The data you're showing is not JSON (it's invalid syntax) so show the (minimal) code and (minimal) input data that results in what you're getting. – Mike 'Pomax' Kamermans Oct 31 '22 at 14:33
  • Please read [ask], provide a [mcve] and give a more detailed description of the behaviour you are seeing. You haven't shown us your input, a short bit of code that demonstrates the problem end to end, or how you are looking at the output (e.g. if you are using something that displays the raw result or something which adds extra characters to show you data types in an object structure). – Quentin Oct 31 '22 at 14:33

1 Answers1

1

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.)

Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
  • That really helps now, thank you @Heiko! Actually JSON.stringify on the whole output converts them too, but I only found it out playing around with your example. – apingaway Oct 31 '22 at 14:44