DB2 to JSON: Tables, Arrays and JSON Lines

One array of objects, or one object per line - numbers kept as numbers, NULL kept as null, and the document columns carried as their text.

Download DBToFile — free trial

Version 3.1.2 · Windows, Linux, macOS · 30-day free trial, then $75 one-time · runs fully offline

DBToFile: DB2 to JSON target - document shape, indentation and NULL handling

How to Export DB2 to JSON, Step by Step

Step 1: Connect to DB2

Host, port (50000 by default for LUW), database and account, then connect. The Test button reports the server version before anything is read.

Step 1 screenshot

Step 2: Choose the table or the query

A single table, every table in a schema, or a SELECT pasted in - the column list comes from what you pick and stays editable.

Step 2 screenshot

Step 3: Set the JSON shape

Delimiter or document options, header row, NULL marker, encoding and the file name - set once and saved with the task. Dynamic file name covers the dated-file case the old export-...-with-current-date posts described.

Step 3 screenshot

Step 4: Run and check the file

The task window reports the rows written; open the file and check the first and last row before anything else consumes it.

Step 4 screenshot

Key Features

DB2 exports are usually batch jobs, so the useful features are the ones that survive a schedule:

  • Table, query, or a whole schema - a batch run writes one file per table, named after the table.
  • Dated file names (orders_#yyyy#-#mm#-#dd#) so a nightly run does not overwrite yesterday.
  • The format settings are saved with the task instead of re-chosen each time.
  • Types are honoured: DECIMAL keeps its scale, TIMESTAMP its date, CLOB is not cut.
  • Saved and scheduled: Task Scheduler, cron or launchd.
  • Local: the file is written where you are, and the DB2 password never leaves the machine.

What a DB2 column becomes in the JSON

A JSON is text, so the interesting part is where each DB2 type loses something on the way out:

In DB2In the JSON
INTEGER, BIGINTa JSON number - beyond 2^53 a JavaScript parser rounds it
DECIMAL(p,s)a JSON number with the declared scale
CHAR, VARCHARa JSON string, escaped
CLOB, DBCLOBa JSON string - the text, escaped
XMLits serialised text inside a string - the structure is not reshaped into members
DATE, TIMESTAMPa string - JSON has no date type
NULLthe JSON literal null
BLOB, FOR BIT DATAnot text - export HEX(col) or the document is invalid

One array, one object per line, or SQL/JSON in the query?

DB2 can build JSON inside a query with JSON_OBJECT and JSON_ARRAY (and JSON_AGG in newer releases). That is the right tool when the document shape is a report; it is not a file writer - the result is one value that has to be fetched in one piece.

On z/OS the utility for getting rows out is called UNLOAD, and it writes delimited files, not JSON - unload data from DB2 in JSON form is exactly the gap this page fills.

Two shapes are what consumers ask for: one array of objects for an API, and one object per line - JSONL/NDJSON - which appends and streams. Both are a setting here, on the same connection that reads the table.

DB2 to JSON — Quick Facts

Database
DB2 for LUW 9.x-12.x, and z/OS through the same connection
Default port
50000 (LUW)
Identifier quoting
"double quotes"; unquoted names fold to UPPERCASE
Shapes
one array of objects, or one object per line (JSONL/NDJSON)
Numbers
BIGINT beyond 2^53 and DECIMAL are at risk in float parsers
Dates
strings - JSON has no date type
XML columns
carried as text, not re-structured
SQL-side alternative
JSON_OBJECT / JSON_ARRAY, built as one value

Common pitfalls when exporting DB2 to JSON

  • A DB2 XML column arrives as a string: a consumer expecting a nested object has to parse it.
  • DECIMAL values written as numbers lose their scale in a float parser - if the scale matters, keep them as strings.
  • A CLOB holding JSON is exported as a string, so the consumer sees double-encoded JSON.
  • One huge array cannot be streamed; JSONL can, and it appends across runs.
  • Unquoted identifiers fold to UPPERCASE, so JSON keys come out as "DEPT_ID" unless the mapping renames them.

Which searches land here

This page exports DB2 → JSON. The neighbouring searches below want another format or another direction:

What you searchedMeaningPage
db2 to json, db2 export to json, db2 query to jsonread DB2, write a JSON filethis page
db2 to jsonl, db2 to json array, ndjson exportone object per line, or one array - both are settings on this pagethis page
db2 to csv, db2 to txt, db2 to tsv, db2 to xml, db2 to sql, db2 to html, db2 to excela different output format from the same connectionCSV / TXT / TSV / XML / SQL / HTML / Excel
csv to db2, import csv into db2the reverse direction - a file into DB2CSV to DB2
db2 export command, db2look, db2movethe server-side commands: EXPORT writes on the DB2 host, db2look is DDL, db2move moves a databasethis page (client export) or the DB2 manual
csv to dbf converter, dbf to csva different pair entirely (DBF, not DB2) - the names are one letter apartCSV to DBF / DBF to CSV

One letter separates DB2 from DBF, and both appear in the same keyword space: the columns are different formats, not different vendors.

Verify three things before you hand the file over

  • Row count: compare SELECT COUNT(*) with the rows the task reports - a short count means a rejected row.
  • One DECIMAL and one TIMESTAMP: the scale should be intact and the timestamp should not have lost its date.
  • One long CLOB and one accented string: the text should be complete and readable in the target application.

Frequently Asked Questions

How do I export DB2 to JSON?
Connect, choose the table or query, pick the shape (array or one object per line), then write the file.
Array or JSONL?
An array suits an API; one object per line appends and streams, which suits logs and big tables.
What happens to a column that already stores a document?
It is carried as its serialised text inside a string - JSON has no XML type, and re-shaping it is a query decision.
Are the types preserved?
Numbers and nulls yes; dates become strings because JSON has no date type.
Can I export only some columns?
Yes - the column list comes from the query you write.

Download Withdata DBToFile Now

Start exporting DB2 to JSON in seconds - free trial for all platforms.

$75 one-time · volume discounts to $55 · 30-day money-back · pricing & licence terms

Download Free Trial