Skip to content

Parse object string

parseObjectString parses JSON object strings into plain JavaScript objects.

It rejects arrays, null, and any non‑object structures to ensure the result is always a valid JSON object. If the value is not a string, does not begin and end with {} braces, fails JSON parsing, or produces a non‑object value, the rule emits no parse result.

Signature

Through the API:

.parse('object');

And internally:

export const parseObjectString: ParseRule<unknown>
(value: unknown, path: FieldPath) => ParseResult<unknown>[]

Events

Event code Description
string.now.object Parsed string into a JSON object.

Design rationale

  • Accepts only canonical JSON object literals:
  • Must start with { and end with }.
  • Must parse cleanly with JSON.parse.
  • Rejects:
  • Arrays ([…]).
  • null.
  • Primitives (string, number, boolean).
  • Malformed or ambiguous JSON.
  • Ensures contributors can rely on strict, predictable object parsing.
  • Produces a single parse step containing the parsed object and one parseEvent.
  • Never guesses, normalizes, or coerces — parsing is explicit and opt‑in.

Invoke

parseObjectString runs only when explicitly included in a boundary or pipeline. It is not affected by normalization mode and does not run automatically.

The rule activates when:

  • The value is a string.
  • The trimmed string begins with { and ends with }.
  • JSON.parse succeeds.
  • The parsed value is a non‑null, non‑array object.

Examples

Valid object string

parseObjectString('{"a": 1, "b": 2}', "$");
// → [
//     {
//       path: "$",
//       nextValue: { a: 1, b: 2 },
//       events: [
//         JaneEvent{ kind: "info", code: "string.now.object", ... }
//       ]
//     }
//   ]

Invalid formats — no parse

parseObjectString("[1, 2]", "$");        // → []
parseObjectString("null", "$");          // → []
parseObjectString('"hello"', "$");       // → []
parseObjectString("{ bad json }", "$");  // → []
parseObjectString("42", "$");            // → []

Non‑string value

parseObjectString({ a: 1 }, "$"); // → []