TSV to JSON Converter

Read every field as text, or read numbers and booleans out of it.
at field what was decided what came out
1:1 name header row · column 1 of 3 "name":
1:6 age header row · column 2 of 3 "age":
1:10 city header row · column 3 of 3 "city":
2:1 Ada record 1 · column "name" · unquoted field · kept as text "name":"Ada"
2:5 36 record 1 · column "age" · unquoted field · kept as text "age":"36"
2:8 London record 1 · column "city" · unquoted field · kept as text "city":"London"
3:1 Grace record 2 · column "name" · unquoted field · kept as text "name":"Grace"
3:7 45 record 2 · column "age" · unquoted field · kept as text "age":"45"
3:10 Paris record 2 · column "city" · unquoted field · kept as text "city":"Paris"

Nothing in this document was dropped or changed.

Going the other way? Convert JSON back to TSV

How do you convert TSV to JSON?

TSV to JSON reads tab-separated records and writes a JSON array: the name line becomes the keys, and every later line becomes one object.

  1. Paste the tab-separated file above. The first line holds the field names, so it names the columns.
  2. Split every later line on the tab characters. There is no quoting to honour, so the field count is the number of tabs plus one.
  3. Pair each field with the name in its column and write one object per record. The result is a JSON array of objects.

This page writes a JSON array of objects with the first line supplying the keys; tabs separate the fields and no field may contain a tab.

TSV to JSON Conversion Table

TSV and CSV look like the same idea and behave differently at exactly three points. A semicolon-separated or tab-separated file is not RFC 4180 CSV. The grammar fixes the separator at the comma (%x2C) and the media type has no delimiter parameter, so any other separator is read by convention rather than by the specification.

TSV to JSON Conversion Table
What it saysTSVCSV
Delimitera tab charactera comma (%x2C), fixed by the RFC 4180 grammar
Quotingnone - a field containing a tab is not allowabledouble quotes around the field; a double quote inside a quoted field is written twice
Name linerequired - the first line holds the field namesoptional
Field countevery record must carry the same number of fieldsthe RFC asks for it and does not enforce it
A tab inside a valuenot allowable, and there is no escape for itordinary text, and no quoting is needed

A format is defined by what it can say. The rows below put the two halves of this conversion side by side, so the gap the conversion has to cross is a thing you can read rather than a thing you find out later. Every value in the table is the wording the defining document uses, not a summary of it, and a dash means the format has no answer to that row at all.

What TSV and JSON Can Say
What it saysTSVJSON
SpecificationIANA media type registration for text/tab-separated-values; no RFC and no standards-body documentRFC 8259, Standards Track, December 2017; also ECMA-404
Media typetext/tab-separated-valuesapplication/json
Structureflat records of fields; no nestingobjects and arrays, nested to any depth
Delimitera tab charactera comma (%x2C) between two members of an object and between two elements of an array
Quotingnone - a field containing a tab is not allowabledouble quotes; a quotation mark, a reverse solidus and the control characters U+0000 to U+001F must be escaped with a reverse solidus
Header linerequired - the first line holds the field names
Field countevery record must carry the same number of fields
Type setnone - every field is textstring, number, boolean, null, object, array
Nestingnoneunlimited by the grammar; a parser may set its own depth limit
Orderingan object is unordered; an array is ordered
Commentnone - the grammar has no comment production
EncodingUTF-8

Worked Example: TSV to JSON

A name line and two records, converted here at build time by the same code the box above runs. The working under it shows every field, where it sat in the file, the key it took and the value it became — three columns and no quoting rules to read, which is the whole argument for tabs.

TSV in
name	age	city
Ada	36	London
Grace	45	Paris
JSON out
[{"name":"Ada","age":"36","city":"London"},{"name":"Grace","age":"45","city":"Paris"}]
at field what was decided what came out
1:1 name header row · column 1 of 3 "name":
1:6 age header row · column 2 of 3 "age":
1:10 city header row · column 3 of 3 "city":
2:1 Ada record 1 · column "name" · unquoted field · kept as text "name":"Ada"
2:5 36 record 1 · column "age" · unquoted field · kept as text "age":"36"
2:8 London record 1 · column "city" · unquoted field · kept as text "city":"London"
3:1 Grace record 2 · column "name" · unquoted field · kept as text "name":"Grace"
3:7 45 record 2 · column "age" · unquoted field · kept as text "age":"45"
3:10 Paris record 2 · column "city" · unquoted field · kept as text "city":"Paris"

Result: the TSV above is the JSON beside it, and every decision that made it is listed below.

The name line is not optional here, and that is the first real difference from CSV. A TSV file's first line is the field names, and the IANA registration puts that line in the grammar as tsv ::= nameline record+. Where CSV's header row is optional, a TSV file without a name line is not TSV.

Every value came out as a JSON string. RFC 8259 gives JSON four primitive types - string, number, boolean and null - and two structured types, object and array. There is no date type and no separate integer type, so a date arrives as a string and every number is read the same way. The type rule above reads the numbers and booleans out when you want them, and reports what that costs.

Check it in reverse

Writing the same records back out as tab-separated rows returns the file you started from, as long as no value picked up a tab or a line break along the way. Those two characters are the only ones a tab-separated file cannot hold, and the converter replaces them rather than writing a file no reader can split. Everything else survives the round trip unchanged, because there is no quoting to undo and no type to guess at: what went in as text comes back as text, in the same order, under the same names.

To prove nothing was lost, convert the JSON result back to TSV and compare it with the file you started from.

What This Conversion Cannot Carry

Tabs remove the quoting problem and add a harder one: there is nowhere to put a tab. TSV has no quoting mechanism. The IANA registration for text/tab-separated-values states that fields containing tabs are not allowable, so a tab inside a value cannot be escaped - it has to be removed or replaced before the file is written.

An empty cell and an empty string
An empty field became the empty string. CSV has no null (FMT-csv-types), so an empty cell, a missing value and an empty string are one thing on the page and cannot be told apart on the way back.
A blank line between records
A blank line stood between two records. RFC 4180 has no production for one, so it was skipped rather than read as a record of one empty field.
A byte order mark
A byte order mark stood at the front of the file. It was removed rather than read as part of the first field.

Two things here are refused rather than reported: a record with the wrong field count, and a name line that names one column twice. RFC 8259 says the names in an object should be unique and records that software receiving a repeated name behaves unpredictably: some implementations keep the last pair, some fail, some keep all of them. An object is unordered, and parsers differ over whether they expose member order at all. Each line above is written by the converter itself and appears under your own result whenever your file raises it.

The sample above reverses exactly - write the same records back out as tab-separated rows .

What Do Tabs Change About the Result?

Whether every record carries the same field count

A record with the wrong number of fields stops the conversion rather than being padded. The IANA registration for TSV requires every record to have the same number of fields, so a JSON array whose objects carry different keys cannot be written as TSV until one column set is fixed for the whole file.

What the values are read as

Every field is text until you say otherwise. A JSON number is written in base 10, leading zeros are not allowed, and Infinity and NaN are not permitted. RFC 8259 lets a parser set its own limits on range and precision, so integers outside -(2^53)+1 to (2^53)-1 are where two parsers start to disagree about the same file.

What the JSON side will not accept back

The output is JSON, so it obeys the JSON grammar exactly. JSON has no comments and no trailing commas because RFC 8259's grammar has no production for either. A comma may appear only between two members of an object or two elements of an array, so a comma before the closing brace or bracket makes the text invalid.

TSV to JSON in Code

The same conversion in two lines, one per language. Both take the first line as the field names and split the rest on the tab. Neither line reports what the conversion could not carry; the converter above does.

Python: json.dumps(list(csv.DictReader(open("in.tsv", newline=""), delimiter="\t")))
JavaScript: lines.slice(1).map(l => Object.fromEntries(head.map((h, i) => [h, l.split("\t")[i]])))

Common Mistakes

Frequently Asked Questions

Does a TSV file have to have a header line?

Yes. The first line holds the field names and the registration puts it in the grammar, so a tab-separated file without one is not TSV. A CSV header row is optional instead.

How is TSV different from CSV?

Three things: the delimiter is a tab, there is no quoting mechanism, and the name line is required. CSV instead uses double quotes around the field; a double quote inside a quoted field is written twice.

Can a TSV field contain a tab character?

No. The registration for text/tab-separated-values says such a field is not allowable, and there is no escape for it, so the tab has to be removed or replaced first.

Do TSV values keep their types in the JSON?

Not on their own. Every field is text, so the default output is JSON strings. Switch the type rule above to read numbers and booleans out, and the converter reports what it cost.

Yes. The same file converts straight back in the JSON to TSV direction .

the JSON, XML, CSV and TSV converters are all on one page.