Skip to content

Conversation

@bean1352
Copy link
Contributor

Added comprehensive guide for querying JSON data in the client SQLite database

  • Explains JSON storage as strings in SQLite when synced from backend (MongoDB, PostgreSQL, MySQL)
  • Documents -> vs ->> operators with clear examples showing quoted vs unquoted output
  • Covers json_extract() and json_each() for flattening and querying nested arrays/objects
  • Includes real-world examples: filtering, aggregating etc
  • Shows delimiter-separated value queries using instr() pattern
  • Performance optimization tips: indexing JSON columns in AppSchema, filtering before expanding, using EXISTS
  • Documents common gotchas: NULL handling, type casting
  • Provides reference for useful JSON functions (json_array_length, json_valid, json_type, etc.)

@bean1352 bean1352 requested review from benitav and rkistner November 26, 2025 10:43
@benitav
Copy link
Collaborator

benitav commented Nov 26, 2025

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 michaelbarnes self-requested a review December 1, 2025 15:28
Copy link
Contributor

@michaelbarnes michaelbarnes left a 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.
Copy link
Contributor

@michaelbarnes michaelbarnes left a 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.
Comment on lines 431 to 438
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
```
Copy link
Contributor

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 ->>

Copy link
Contributor Author

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.

1. **NULL vs missing keys**: `json_extract()` returns `NULL` for non-existent paths. Always check for NULL:
```sql
WHERE COALESCE(metadata ->> '$.priority', '999') = '1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WHERE COALESCE(metadata ->> '$.priority', '999') = '1'
WHERE COALESCE(metadata ->> '$.priority', 999) = 1

Comment on lines +423 to +424
-- ✅ Cast to numeric type
WHERE CAST(metadata -> '$.priority' AS INTEGER) > 5
Copy link
Contributor

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)
Copy link
Contributor

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,
Copy link
Contributor

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?

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.

5 participants