prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

Cannot convert value to JSON: '%s'

What it means

jsonParse validates that the input slice is well-formed JSON by parsing it with Jackson; any parse failure (malformed JSON, bad encoding, trailing characters) is rethrown as a PrestoException with code INVALID_FUNCTION_ARGUMENT. This matches Presto's convention for JSON functions receiving invalid user input.

Source

Thrown at presto-plugin-toolkit/src/main/java/com/facebook/presto/plugin/base/JsonTypeUtil.java:57

    private static final ObjectMapper SORTED_MAPPER = new JsonObjectMapperProvider().get().configure(ORDER_MAP_ENTRIES_BY_KEYS, true);

    private JsonTypeUtil() {}

    public static Slice jsonParse(Slice slice)
    {
        // cast(json_parse(x) AS t)` will be optimized into `$internal$json_string_to_array/map/row_cast` in ExpressionOptimizer
        // If you make changes to this function (e.g. use parse JSON string into some internal representation),
        // make sure `$internal$json_string_to_array/map/row_cast` is changed accordingly.
        try (JsonParser parser = createJsonParser(JSON_FACTORY, slice)) {
            SliceOutput dynamicSliceOutput = new DynamicSliceOutput(slice.length());
            SORTED_MAPPER.writeValue((OutputStream) dynamicSliceOutput, SORTED_MAPPER.readValue(parser, Object.class));
            // nextToken() returns null if the input is parsed correctly,
            // but will throw an exception if there are trailing characters.
            parser.nextToken();
            return dynamicSliceOutput.slice();
        }
        catch (IOException | RuntimeException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Cannot convert value to JSON: '%s'", slice.toStringUtf8()), e);
        }
    }
    private static JsonParser createJsonParser(JsonFactory factory, Slice json)
            throws IOException
    {
        // Jackson tries to detect the character encoding automatically when
        // using InputStream, so we pass an InputStreamReader instead.
        return factory.createParser(new InputStreamReader(json.getInput(), UTF_8));
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Fix the source data so the value is valid JSON (proper quoting, no single quotes, no trailing commas)
  2. Guard the call with a TRY clause: TRY(json_parse(col)) to get NULL instead of a query failure
  3. Pre-validate with json_format/json_extract or a JSON validity check before parsing
  4. Check character encoding — the slice must be UTF-8 text

Example fix

// before
SELECT json_parse(raw) FROM events;
// after
SELECT TRY(json_parse(raw)) FROM events; -- NULL for invalid rows instead of query failure
Defensive patterns

Strategy: validation

Validate before calling

-- SQL-level guard
SELECT CASE WHEN json_valid(raw) THEN json_parse(raw) END FROM t;
-- or
SELECT TRY(json_parse(raw)) FROM t;

Prevention

When it happens

Trigger: Calling a JSON function (e.g. json_parse / json_extract on a varchar) whose argument is not valid JSON: missing quotes around strings, unescaped characters, empty string, concatenated JSON values, or binary garbage in a varbinary-derived slice.

Common situations: Columns storing half-written or truncated JSON; CSV imports where JSON fields were not escaped; upstream systems writing Python-style dicts with single quotes instead of valid JSON.

Related errors


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