-
Notifications
You must be signed in to change notification settings - Fork 11
Add SQLite JSON querying guide #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
this is great! 🔥 One suggestion I have is to link to this reference from other places in the docs where this use case would come up, e.g. two that come to mind are https://docs.powersync.com/usage/use-case-examples/custom-types-arrays-and-json (maybe even consolidate these 2 pages as there seems to be a lot of overlap), and where we mention JSON on https://docs.powersync.com/usage/sync-rules/types |
…ate text column reference and add links for further information on JSON handling and database type mapping.
michaelbarnes
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this is a great addition. I've left a few comments around wording and a few links back to SQLite docs for easy reference.
… links to relevant SQLite functions for better user guidance.
michaelbarnes
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few small changes to be consistent.
…ON functions, corrected example references to 'tasks', and added a link to the SQLite JSON functions for enhanced user guidance.
| 2. **Type mismatches**: JSON numbers are returned as text with `->`. Always cast for numeric operations: | ||
| ```sql | ||
| -- ❌ String comparison (wrong!) | ||
| WHERE metadata -> '$.priority' > 5 | ||
|
|
||
| -- ✅ Numeric comparison (correct!) | ||
| WHERE CAST(metadata -> '$.priority' AS INTEGER) > 5 | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here - should just use ->>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep this in the Common Gotchas section since it shows that using ->> is better than -> with CAST. I've changed the other examples throughout the doc to use ->>, and added a "BEST" option below that demonstrates ->> as the recommended approach.
…escriptions, updating example queries for clarity, and correcting references to JSON functions. Added SQL Server JSON column type and improved formatting for better readability.
…tion for numeric values.
|
|
||
| 1. **NULL vs missing keys**: `json_extract()` returns `NULL` for non-existent paths. Always check for NULL: | ||
| ```sql | ||
| WHERE COALESCE(metadata ->> '$.priority', '999') = '1' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| WHERE COALESCE(metadata ->> '$.priority', '999') = '1' | |
| WHERE COALESCE(metadata ->> '$.priority', 999) = 1 |
| -- ✅ Cast to numeric type | ||
| WHERE CAST(metadata -> '$.priority' AS INTEGER) > 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest removing the CAST recommendation - there's no good reason to use that rather than ->>.
| ```sql | ||
| -- Using -> (returns JSON) | ||
| SELECT metadata -> '$.priority' FROM tasks; | ||
| -- Result: '1' (as JSON string with quotes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a JSON string, but won't have quotes.
| SELECT | ||
| id, | ||
| title, | ||
| metadata -> '$.priority' AS priority, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better example we can use for the -> operator?
Added comprehensive guide for querying JSON data in the client SQLite database
->vs->>operators with clear examples showing quoted vs unquoted outputjson_extract()andjson_each()for flattening and querying nested arrays/objectsinstr()pattern