Function: parsePartialJson()
@providerprotocol/ai / utils / parsePartialJson
Function: parsePartialJson()
Section titled “Function: parsePartialJson()”parsePartialJson<
T>(json):PartialParseResult<T>
Defined in: src/utils/partial-json.ts:217
Parses potentially incomplete JSON, returning as much as can be extracted.
Handles common incomplete states during streaming:
- Incomplete strings:
{"name":"Jo→{name: "Jo"} - Incomplete objects:
{"a":1,"b":→{a: 1} - Incomplete arrays:
[1,2,→[1, 2] - Incomplete numbers, booleans, null literals
- Nested structures with partial completion
- Unicode escape sequences
Type Parameters
Section titled “Type Parameters”T = unknown
The expected type of the parsed value
Parameters
Section titled “Parameters”string
The potentially incomplete JSON string
Returns
Section titled “Returns”A PartialParseResult with the parsed value and completion status
Example
Section titled “Example”// Complete JSONparsePartialJson('{"name":"John"}');// => { value: { name: "John" }, isComplete: true }
// Incomplete objectparsePartialJson('{"user":{"firstName":"Jo');// => { value: { user: { firstName: "Jo" } }, isComplete: false }
// Incomplete arrayparsePartialJson('[1, 2, 3');// => { value: [1, 2, 3], isComplete: false }