Skip to content

JSON-aliased VARCHAR columns are returned as escaped strings instead of nested JSON in API responses #38

Closed

Description

@ritaalves-zoi

Summary

When an endpoint returns a column whose DuckDB type is JSON (a logical-type alias over VARCHAR), flAPI serialises it as a JSON-escaped string in the HTTP response instead of embedding the parsed object/array. The consumer then has to do a second JSON.parse on the string, which defeats the point of typing the column as JSON.

Reproduction

Minimal endpoint:

sqls/json_demo.yaml
​```yaml
url-path: /json-demo
template-source: json_demo.sql
connection:

  • duckdb-local
    ​```

sqls/json_demo.sql
​sql SELECT 1 AS id, '{"a": 1, "b": [10, 20], "c": {"nested": true}}'::JSON AS payload ​

GET /json-demo returns:

​json { "data": [ { "id": 1, "payload": "{\"a\":1,\"b\":[10,20],\"c\":{\"nested\":true}}" } ], "total_count": 1, "next": "" } ​

Expected:

​json { "data": [ { "id": 1, "payload": { "a": 1, "b": [10, 20], "c": { "nested": true } } } ], "total_count": 1, "next": "" } ​

Our actual data is a recursive tree (spare parts with children arrays), so we can't really hand-write a from_json schema for it. The source column in BigQuery is typed JSON, and we'd appreciate for the API to just return it as JSON.

(Probable) Root cause

In src/query_executor.cpp, QueryResult::convertVectorEntryToJson dispatches on duckdb_get_type_id(type), which is the physical type id. DuckDB's JSON type is a logical alias over VARCHAR, so the dispatch picks the DUCKDB_TYPE_VARCHAR branch:

​cpp case DUCKDB_TYPE_VARCHAR: return convertVectorVarcharToJson(vector, row_idx); ​

convertVectorVarcharToJson returns:

​cpp return crow::json::wvalue(str); ​

Crow then JSON-escapes that string when serialising the response, producing the behaviour above.

flAPI already serialises native nested types correctly (convertVectorStructToJson, convertVectorListToJson, convertVectorMapToJson), so the only thing missing is detection of the JSON logical-type alias before falling through to VARCHAR.

Environment

  • flAPI: main (also reproduced on the latest release at the time of writing)
  • Connection: bigquery-lakehouse (read-only)

Thanks in advance!

Activity

  1. jrosskopf commented on May 21, 2026

    @jrosskopf
    Contributor

    Hi @ritaalves-zoi — thanks so much for the report, and thanks for using flAPI! The write-up was exemplary: minimal reproduction, expected vs. actual response, and a root-cause analysis that pointed straight at the dispatch site in convertVectorEntryToJson. That made the fix a tight TDD loop rather than a hunt.

    The fix is now on main via #39 and will ship in the next tag (v26.05.20). JSON-aliased VARCHAR columns are now embedded as nested JSON, so your recursive parts tree should come back through the API ready to consume without a second JSON.parse. Malformed JSON in a source row degrades gracefully to the raw string instead of nulling the cell, and a pre-existing per-cell logical-type leak in the same function got cleaned up along the way.

    If you bump into anything else - bug, papercut, or just an idea for making flAPI nicer to develop against or to deploy (CLI ergonomics, project scaffolding, observability, containerization story, CI integration, whatever has been slowing your team down) - please open an issue. We genuinely value field-driven feedback like this; it's how the rough edges get found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions