Skip to content

When JSON should be a listing and when a table

JSON comes in two shapes. Some files are nested, with objects inside objects, like a configuration or a single API response, and read best as an indented listing. Others are a long list of records that all share the same keys, which is a table in all but name. Knowing which you have decides which layout to choose.

Updated

Which tool you need

  • A list of orders, users or rows exported from an API

    Set it as a table. Each key becomes a column, and the column names repeat at the top of every page, so page twelve can be read without turning back to page one.

  • A nested configuration or a single response

    Keep the listing. The nesting is what the indentation shows, and a table would have to flatten it away.

  • A file a program says is invalid

    Convert it anyway: the listing names the fault at the top, which points at a missing comma or bracket faster than reading the file end to end.

  • The same data arrives as XML

    XML to PDF lists an XML file the same way, one element to a line.

    XML to PDF

Why the numbers are copied, not converted

Many programs that read JSON turn every number into a floating-point value, and a long identifier such as an order or account number quietly loses its last digits on the way. The listing here prints each value from the text of the file itself, so 9007199254740993 stays 9007199254740993.

Keys keep the order the file gives them, and strings keep their spelling. Nothing is sorted or reformatted beyond the indentation, so the pages can be compared with the file line by line.

When the records can become a table

The table is offered only when the whole file is a list of records, or JSON Lines holding two or more, and every value in every record is a string, a number, true, false or null. One nested object or array in a single record keeps the file a listing, and so does a fault anywhere in it. A single object, such as a configuration, is never offered as a one-row table.

Columns follow the order in which keys first appear, and a record without a key leaves that cell empty. In the table a string loses its quotation marks and its escapes are read, so \u00e9 shows as é, while numbers are still copied as written and a column holding nothing but numbers is right-aligned. Line numbers are not offered alongside a table.

How the JSON listing is laid out

The listing is Courier on a shaded ground, indented two spaces for each level, with one member to a line and a space after every colon. An empty {} or [] stays on one line, and in a JSON Lines file each value starts on a fresh line of its own.

The fault named at the top comes from two readers in turn. The listing's own reports the first thing that cannot be JSON, such as a word without quotes or a string or bracket left open; when it finds nothing, your browser's JSON parser reads the file and catches the rest, a trailing comma among them.

What to know before you start

  • Data nested inside the records keeps a table from being offered; it stays a listing.
  • Keys, values and punctuation share one color.
  • Text in a script the chosen Fonts level does not include, and any emoji, shows as a replacement mark.

Read next