STRUCTURED TEXT

Preserving leading zeros when converting CSV identifiers

Identifiers such as 00123 contain digits without being quantities. The CSV to JSON converter returns every field as a string, so the leading zeros come through intact.

Digits that name things rather than count them

Many codes are written with digits yet are labels: postal codes, product numbers, account references, employee IDs and part numbers. Their leading zeros carry information. United States ZIP codes in New England, for example, begin with 0, and dropping that digit leaves a four-digit value that is not a valid ZIP code. The postal code 02134 and the number 2134 are different identifiers, and arithmetic on them has no meaning, since adding two part numbers produces nothing useful. A conversion step that treats such a field as a quantity can silently rewrite it. The CSV converter avoids that by keeping every field as text exactly as it appears in the source.

Reading the output for 00123

Enter id on the first line and 00123 on the second with header mode on. The output is a single object with the property id, and its value is shown in double quotes as "00123". The quotation marks are JSON's way of marking a string, and the output is indented by two spaces for readability. The value has five characters, so the two leading zeros are retained. A value such as 0 or 000 would be kept the same way, as would an identifier with letters mixed among the digits, like A00123. If the value had been converted to a number, the output would show 123 without quotes. The presence of quotes is the quickest visual check that the converter did not apply any numeric interpretation.

A decimal-looking field stays text too

The same rule applies to amounts. With amount on the first line and 12.50 on the second, the output is "12.50" in quotes, with the trailing zero in place. A numeric conversion would normally print 12.5 and drop that zero. The converter does no type inference and no decimal rewriting, so what you see is what the source held. The same holds for a value like 1,250.00 once it is quoted, which stays one string with its comma and trailing zeros, because thousands separators belong to the text and are not interpreted. This matters for money values formatted to two decimals, version strings like 1.10, which must not become 1.1, and scientific-looking codes such as 1E5, which are sometimes labels.

Where numbers lose their zeros

Leading zeros usually disappear in a later step, not in the CSV itself. Opening a CSV file directly in a spreadsheet application commonly converts a column of digits to numbers and drops the zeros, and saving the file back writes the shortened values. Code that parses fields with a numeric function, or a database column declared as an integer, does the same. The converted JSON from this page will carry the zeros, so the risk begins when you pass the strings to a system that applies its own types. Check that destination before importing. A quick test is to place one value with a leading zero in a trial file and watch what the destination shows after loading.

Choosing the type in the receiving system

Decide column by column what each field is. Labels such as postal codes, phone numbers, SKU codes and national identifiers should stay strings in the target schema. Measurements such as quantity, price and weight can become numbers there, with the conversion done deliberately and with the number of decimal places specified. The converter does not make that decision, because digit-shaped text does not reveal intent. A column of 007, 042 and 105 could be codes or counts, and only the owner of the data can say which.

Padding and width as part of the identifier

Fixed-width identifiers rely on their zeros to keep a consistent length so that sorting as text matches the intended order. As strings, 00009 sorts before 00010, while the unpadded strings 9 and 10 sort as 10 before 9. If an upstream step removed the padding, restoring it requires knowing the intended width, and padding to five digits would be a guess unless the specification says so. Keeping the original strings avoids needing that repair. Barcodes work the same way: a 12-digit UPC-A code that begins with 0 is still a 12-digit code, and trimming it to 11 digits produces a different, invalid identifier. For files with multiple identifier columns, use the page on a small sample first and compare the zeros against the source.

A mixed example

Try zip and qty on the first line, then 02134,007 on the second with header mode on. Both values come back as strings with zeros intact: "02134" and "007". Whether 007 is a quantity or a code is your decision for the receiving system. If it is a quantity, the target can convert it to 7 after import. If it is a code, the string must stay. Making that choice at the destination keeps the information available until you know which is correct. Discarding zeros early cannot be undone from the data alone, while adding a numeric type later is always possible.

Limits of the converter's guarantee

The converter preserves the content of each field. It does not infer your schema, validate identifiers against a format, or check that a field has the length it should. A string of 00123 passes through exactly as typed, including a typo. The examples show retention of text and make no claim about the correctness of any identifier. A check digit, a required prefix or a fixed length would each need a separate validation step in your own pipeline.

Handing identifiers to a spreadsheet or database safely

If the converted data goes on to a spreadsheet, import it through a dialog that lets you set the column type to text before the values are read. Opening the file by double-clicking skips that choice. In a database, declare identifier columns as character types such as varchar and leave integer types for counts. In code, keep the field as a string through every step and apply padding or formatting only at the point of display. Where you must store a number, record the field width separately, so a postal code of 02134 can be rebuilt from 2134 and a width of 5 rather than guessed. Test the round trip with a file that holds a leading-zero value before relying on it for thousands of rows, and compare the exported text with the original line by line.

Reproduce the checks

Identifier with leading zeros

Open the Csv To Json Converter and enter:

CSV input
id
00123
First row contains headers
true

Expected output:

Rows

[
  {
    "id": "00123"
  }
]

The identifier remains a five-character string.

A decimal-looking field also stays text

Open the Csv To Json Converter and enter:

CSV input
amount
12.50
First row contains headers
true

Expected output:

Rows

[
  {
    "amount": "12.50"
  }
]

No numeric type inference or decimal rewriting occurs.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks