CodexBloom - Programming Q&A Platform

SQLite: Why Does My Query with JSON Extraction Return NULL for Valid Keys?

👀 Views: 94 đŸ’Ŧ Answers: 1 📅 Created: 2025-07-11
sqlite json sql SQL

I'm working on an application using SQLite version 3.35.5, and I've encountered an scenario with JSON extraction that I need to seem to resolve... I have a table `users` where one of the columns, `data`, stores JSON strings. Here's how the table is structured: ```sql CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT, data TEXT ); ``` I've inserted a few records like this: ```sql INSERT INTO users (name, data) VALUES ('Alice', '{"age": 30, "city": "New York"}'); INSERT INTO users (name, data) VALUES ('Bob', '{"age": 25, "city": "Los Angeles"}'); ``` Now, when I try to extract the `age` of each user using the following query: ```sql SELECT name, json_extract(data, '$.age') AS age FROM users; ``` The result returns NULL for the `age` column. The output looks like this: ``` name | age ---- | ---- Alice| NULL Bob | NULL ``` I've double-checked that the JSON is correctly formatted, and even validated it with an online JSON validator. I also tried using the `json_valid(data)` function to ensure the JSON structure is correct, and it returns `1`, which indicates it's a valid JSON string. Additionally, I've confirmed the column `data` is of type TEXT and contains the expected JSON string. I'm using the `json_extract` function as per the SQLite documentation, but I need to understand why it's returning NULL. Could there be any settings or behaviors in SQLite that I'm missing here? Is there an scenario with the way I'm handling JSON in my queries? Any guidance would be much appreciated! This is part of a larger desktop app I'm building.