Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Several services receive large JSON files from partners and each builds its own parsing code, some with tree models that run out of memory. What standard would you define?

The standard says: anything that may exceed a few megabytes is parsed with a streaming parser (Jackson's JsonParser or Gson's JsonReader), and tree models are allowed only for small documents. Every parser runs with limits on document size, nesting depth and string length, such as Jackson's StreamReadConstraints, to defend against hostile input. A shared component streams items, validates them and reports errors with positions; partner feeds should prefer newline-delimited JSON so files can be split and processed in parallel; and CI tests every consumer with very large and malformed files.

Why this question is asked

When each team writes its own parsing code, each team relearns the same lessons, usually in production. One service reads a 2 GB partner file into a JsonNode tree and dies with OutOfMemoryError; another accepts a document nested a million levels deep and crashes with StackOverflowError; a third reports "invalid JSON" without saying where. A senior engineer's job here is to turn those lessons into a standard that is easy to follow. Interviewers ask this design question to see whether we can set clear rules with reasons, back them with shared code, and make them checkable. In this answer, we will set the rule for choosing a parsing style, add input limits, design the shared component, push for a better file format, and define the tests.

1Rule 1: Stream Anything That Can Grow

There are three ways to parse JSON. Data binding turns the whole document into objects, a tree model such as JsonNode or JsonElement turns it into a generic tree, and streaming reads one token at a time. The first two keep the entire document in memory at once, typically several times its size on disk. Streaming keeps only the current item.

So the rule is about size, not taste. If an input can exceed a few megabytes, now or as the partner grows, it must be processed with a streaming parser, Jackson's JsonParser or Gson's JsonReader, binding one item at a time. Tree models and whole-document binding are fine for small documents such as API requests, configuration and webhooks. When in doubt, a file from a partner counts as "can grow".

Choosing how to parse
InputTypical sizeRequired approach
API request or webhookkilobytesdata binding or tree
Configuration filekilobytesdata binding
Partner export or batch filemegabytes to gigabytesstreaming, one item at a time
Unknown or untrusted sizeunboundedstreaming plus limits

2Rule 2: Always Parse With Limits

Hostile or broken input does not have to be big to hurt. A string of 500 MB in a single value, a number with a million digits, or arrays nested 100,000 deep can exhaust memory or the stack while the document as a whole looks harmless. So every parser in the company runs with explicit limits.

Jackson provides StreamReadConstraints, added in the 2.15 line, which limits nesting depth, number length, string length and, in later versions, total document length. Jackson has sensible defaults for some of these, but we set our own values in the shared configuration so they are documented and consistent. Gson's JsonReader has fewer built-in limits, and newer versions add a nesting limit; check your version. Where the parser lacks a limit, the shared component enforces it, for example by counting bytes on the input stream.

Java
JsonFactory factory = JsonFactory.builder()
.streamReadConstraints(StreamReadConstraints.builder()
.maxNestingDepth(64) // our documents never nest deeper
.maxStringLength(1_000_000) // 1 MB per string value
.maxNumberLength(100) // digits in one number
.maxDocumentLength(5_000_000_000L) // 5 GB per file; check your version for this one
.build())
.build();
// a document nested 65 levels deep now fails fast with StreamConstraintsException

3Rule 3: Use the Shared Streaming Component

Writing a correct streaming loop with error handling and restarts is fiddly, so teams should not write their own. The platform team provides one component that every consumer uses. It opens the input as UTF-8 with buffering, applies the limits, iterates the top-level array or the lines of a newline-delimited file, binds each item to the consumer's class, validates it with Bean Validation, and hands valid items to a callback in batches.

The most important feature is error reporting. Every error names the file, the item index, and the JSON path with line and column, for example items[48213].amount at line 48214, column 37, and says whether it was a syntax error, which stops the file, or a validation error, which is recorded and skipped. Consistent errors mean operations can tell a partner exactly what to fix, and each consumer team writes only its mapping and business logic.

Java
FeedReader.of(Transaction.class)
.limits(Limits.partnerDefault())
.batchSize(1_000)
.onBatch(repository::insertBatch)
.onInvalid(err -> errors.record(err.index(), err.path(), err.message()))
.read(path);
// Feed done: 3,104,557 items, 12 invalid (see error table), 0 syntax errors
Step throughA feed processed by the shared component
Consumer serviceFeedReaderStreaming parserRepository
read(path) with class and callbacks
next item, within limits
one bound Transaction
validate; record invalid with path
insertBatch(1,000 items)
summary: items, invalid, position

6 messages in order. Play them, or step through them one at a time.

4Rule 4: Prefer Newline-Delimited JSON for Feeds

A single 3 GB JSON array has a structural weakness: it is one value, so it can only be parsed from start to end by one thread, and a single broken byte can make everything after it unreadable. Newline-delimited JSON, often called NDJSON or JSON Lines, puts one complete JSON object on each line, with no surrounding array.

That small change has big effects. A file can be split at any newline and processed in parallel. A malformed line affects only that line, so the reader can report it and continue with the next. Restarting means seeking to a line number. And producers can append to the file as they go. So for new partner feeds, the standard asks for NDJSON, and for existing ones we raise it with the partner when the contract next changes.

5Rule 5: Test With Big and Broken Files in CI

A standard that is not tested decays. Every consumer's CI runs the shared component against a set of generated files: a large one, a few hundred megabytes, run with a deliberately small heap such as -Xmx256m to prove memory stays flat; a deeply nested document and an enormous string to prove the limits trigger; a truncated file and one with a syntax error in the middle to check the error message and position; and a file with invalid items to check they are recorded and skipped. These files are generated by a script rather than stored, so the repository stays small.

6Follow-up questions

Isn't streaming harder for developers?

Only if each team writes the loop. With the shared component, consumers write a class for one item and a callback, which is no harder than data binding. That is the point of providing code with the standard, not just a document.

What limit values would you choose?

Values that fit our real data with a margin, rather than library defaults. We measure the deepest nesting and the longest strings in real feeds, then set limits a few times higher. Different input types can have different profiles, for example tighter limits for public APIs than for trusted internal files.

How do you get existing services to adopt it?

New consumers must use it, enforced in review and by a dependency check. Existing ones migrate first where the risk is highest, the services that parse the largest or least trusted inputs, and especially any that already had an out-of-memory incident.

7Conclusion

In this answer, we defined a standard for large JSON inputs: streaming parsers for anything that can exceed a few megabytes and tree models only for small documents, explicit limits on document size, nesting depth and string length such as Jackson's StreamReadConstraints, one shared component that streams, validates and reports errors with positions, newline-delimited JSON for partner feeds so files can be split and recovered, and CI tests with very large and malformed files under a small heap.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write a short standard and back it with shared code. Rule one is about size: anything that can exceed a few megabytes, which includes every partner file, is parsed with a streaming parser, Jackson's JsonParser or Gson's JsonReader, binding one item at a time.

Then, if they want more

Tree models like JsonNode and whole-document binding are only for small documents such as API requests. Rule two is that every parser runs with limits, because hostile input does not need to be big to hurt: maximum nesting depth, string length, number length and document size, which in Jackson is StreamReadConstraints, set explicitly in one shared configuration.

Rule three is that teams do not write their own loops: the platform provides a component that opens the input as UTF-8, applies the limits, streams items, validates them, calls the team's callback in batches, and reports every error with the item index and JSON path, separating syntax errors, which stop the file, from validation errors, which are recorded and skipped.

Rule four is to ask partners for newline-delimited JSON, so files can be split, processed in parallel, and a bad line does not ruin the rest. And rule five is CI tests with large files under a small heap, deep nesting, huge strings and truncated files.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Several services receive large JSON files from partners and each builds its own parsing...": https://scrivio-interview-library.netlify.app/java/json-libraries/7cbe07c9fa/