Skip to content

Fix json example#1313

Merged
mattn merged 1 commit into
mattn:masterfrom
Jaculabilis:json-example
Mar 16, 2026
Merged

Fix json example#1313
mattn merged 1 commit into
mattn:masterfrom
Jaculabilis:json-example

Conversation

@Jaculabilis

Copy link
Copy Markdown
Contributor

Because of sqlite's flexible typing, even though the column was declared as jsonb, the values are stored as the TEXT-typed json because they're converted to strings by Value/Scan. If the table is strict with a BLOB column, the example fails because of the type mismatch. This can be fixed by using the jsonb() function to convert incoming string-typed json and the json() function to convert outgoing binary-typed jsonb. The example is expanded to show both of these approaches.

Note that both approaches use the same string-typed marshalling functions because the conversion to jsonb occurs within sqlite3, not within the Go code. SQLite docs state that the binary format is internal and applications shouldn't try to generate it: https://sqlite.org/json1.html#jsonb

Fixes #1312.

Because of sqlite's flexible typing, even though the column was declared as jsonb, the values are stored as the TEXT-typed json because they're converted to strings by Value/Scan. If the table is strict with a BLOB column, the example fails because of the type mismatch. This can be fixed by using the `jsonb()` function to convert incoming string-typed json and the `json()` function to convert outgoing binary-typed jsonb. The example is expanded to show both of these approaches.

Note that both approaches use the same string-typed marshalling functions because the conversion to jsonb occurs within sqlite3, not within the Go code. SQLite docs state that the binary format is internal and applications shouldn't try to generate it: https://sqlite.org/json1.html#jsonb
@mattn mattn merged commit 57e5007 into mattn:master Mar 16, 2026
@mattn

mattn commented Mar 16, 2026

Copy link
Copy Markdown
Owner

Thank you

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_examples/json is misleading

2 participants