prestodb/presto · error · JsonCastException

INVALID_CAST_ARGUMENT

INVALID_CAST_ARGUMENT

Error message

Expected a json array or object, but got %s

What it means

Cast error from the JSON-to-row cast: after skipping a leading null, the parser found a top-level JSON token that is neither an array (positional row fields) nor an object (named row fields). The %s names the token actually found; the JSON text being cast is the faulty input.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonToRowCast.java:104

                ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
                methodHandle);
    }

    @UsedByGeneratedCode
    public static Block toRow(
            RowType rowType,
            BlockBuilderAppender[] fieldAppenders,
            SqlFunctionProperties properties,
            Slice json)
    {
        try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
            jsonParser.nextToken();
            if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
                return null;
            }

            if (jsonParser.getCurrentToken() != START_ARRAY && jsonParser.getCurrentToken() != START_OBJECT) {
                throw new JsonCastException(format("Expected a json array or object, but got %s", jsonParser.getText()));
            }

            BlockBuilder rowBlockBuilder = rowType.createBlockBuilder(null, 1);
            parseJsonToSingleRowBlock(
                    jsonParser,
                    (SingleRowBlockWriter) rowBlockBuilder.beginBlockEntry(),
                    fieldAppenders,
                    rowType,
                    properties);
            rowBlockBuilder.closeEntry();

            if (jsonParser.nextToken() != null) {
                throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
            }
            return rowType.getObject(rowBlockBuilder, 0);
        }
        catch (PrestoException | JsonCastException e) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s%n%s", rowType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure the JSON is an array of field values (e.g. [1,"a"]) or an object keyed by field names
  2. Validate JSON shape with json_format/json_typeof before casting
  3. Match the number/order of fields to the target row type
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonToRowCast.java:104 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/d3c837b6ab2defa9. Report an issue: GitHub.