Parse a Truncated Streaming JSON Prefix of Strings and Nested Arrays

Quick Overview

Write a function that turns a truncated prefix of streamed JSON, made only of strings and nested arrays, into the value received so far. It tests careful parsing of half-received strings, unclosed arrays and input that ends after a comma, along with writing your own edge-case tests.

Parse a Truncated Streaming JSON Prefix of Strings and Nested Arrays

Company: Listen Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

A language model streams its output as JSON, and the client wants to show what has arrived so far without waiting for the stream to finish. Given the text received so far, which may stop at any character, return the value it represents with everything still unfinished closed off. The JSON contains only strings and arrays that can be nested; there are no objects, numbers, booleans or nulls. The text can stop partway through a string, which keeps only the characters received so far. It can stop while arrays are still open, and it can stop right after a comma. In the interview you also wrote your own tests for the function, so plan the cases you would check. ### Function Signature ```python def parse_partial_json(text: str) -> str | list: ``` ### Rules - `text` is a prefix of a valid JSON value built only from strings and arrays, possibly the whole value. It can end anywhere: inside a string, right after an opening quote, right after `[`, right after a comma, or in the spaces between tokens. - Return each array as a Python `list` and each string as a Python `str`, in the order they appear. - A string whose opening quote has arrived is part of the result, holding the characters received so far. If none have arrived yet, it is the empty string `""`. - A comma with no value after it yet adds nothing to its array. - Every array that has been opened but not yet closed is closed in the result. - The top-level value can itself be a string: `'"hel'` returns `"hel"`. - If `text` is a complete value, return that value unchanged. ### Constraints - `1 <= len(text) <= 10^5` - `text[0]` is `[` or `"`. - Characters inside a string are printable ASCII (codes 32 to 126) other than `"` and `\`, so there are no escape sequences. - Outside strings, the only characters are `[`, `]`, `,`, `"` and the space character. - Arrays are nested at most 500 levels deep. ### Examples **Example 1** ```text Input: text = '["alpha", ["be' Output: ["alpha", ["be"]] ``` The second string has received only `be`, and both arrays are still open. **Example 2** ```text Input: text = '[["x", "y"], "z", ' Output: [["x", "y"], "z"] ``` The text stops after a comma and a space, so the outer array gets no third element. **Example 3** ```text Input: text = '[[], ["' Output: [[], [""]] ``` The first inner array is complete and empty. The second has an opening quote and no characters yet, so it holds one empty string.

Overview: Write a function that turns a truncated prefix of streamed JSON, made only of strings and nested arrays, into the value received so far. It tests careful parsing of half-received strings, unclosed arrays and input that ends after a comma, along with writing your own edge-case tests.

Listen Labs logo
Listen Labs
Oct 20, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
1
0

A language model streams its output as JSON, and the client wants to show what has arrived so far without waiting for the stream to finish. Given the text received so far, which may stop at any character, return the value it represents with everything still unfinished closed off. The JSON contains only strings and arrays that can be nested; there are no objects, numbers, booleans or nulls.

The text can stop partway through a string, which keeps only the characters received so far. It can stop while arrays are still open, and it can stop right after a comma. In the interview you also wrote your own tests for the function, so plan the cases you would check.

Function Signature

def parse_partial_json(text: str) -> str | list:

Rules

  • text is a prefix of a valid JSON value built only from strings and arrays, possibly the whole value. It can end anywhere: inside a string, right after an opening quote, right after [ , right after a comma, or in the spaces between tokens.
  • Return each array as a Python list and each string as a Python str , in the order they appear.
  • A string whose opening quote has arrived is part of the result, holding the characters received so far. If none have arrived yet, it is the empty string "" .
  • A comma with no value after it yet adds nothing to its array.
  • Every array that has been opened but not yet closed is closed in the result.
  • The top-level value can itself be a string: '"hel' returns "hel" .
  • If text is a complete value, return that value unchanged.

Constraints

  • 1 <= len(text) <= 10^5
  • text[0] is [ or " .
  • Characters inside a string are printable ASCII (codes 32 to 126) other than " and \ , so there are no escape sequences.
  • Outside strings, the only characters are [ , ] , , , " and the space character.
  • Arrays are nested at most 500 levels deep.

Examples

Example 1

Input:  text = '["alpha", ["be'
Output: ["alpha", ["be"]]

The second string has received only be, and both arrays are still open.

Example 2

Input:  text = '[["x", "y"], "z", '
Output: [["x", "y"], "z"]

The text stops after a comma and a space, so the outer array gets no third element.

Example 3

Input:  text = '[[], ["'
Output: [[], [""]]

The first inner array is complete and empty. The second has an opening quote and no characters yet, so it holds one empty string.