1

I am trying to retrieve a single item from a JSON array in Sqlite using a predicate.

This is my sample data set:

{
    "book": [
        {
            "author": "Nigel Rees",
            "category": "reference",
            "price": 8.95,
            "title": "Sayings of the Century"
        },
        {
            "author": "Herman Melville",
            "category": "fiction",
            "isbn": "0-553-21311-3",
            "price": 8.99,
            "title": "Moby Dick"
        },
        {
            "author": "J.R.R. Tolkien",
            "category": "fiction",
            "isbn": "0-395-19395-8",
            "price": 22.99,
            "title": "The Lord of the Rings"
        }
    ]
}

I would like to retrieve only the item where the price is 22.99.

In XPath I would do something like this: //book[@price='22.99']

I tried some JSON path evaluators online (https://jsonpath.com/) and this should be working: $.book[?(@.price == 22.99)]

However, when I try to use the same path in a SELECT query in Sqlite, it throws the following exception:

Result: JSON path error near '[?(@.price == 22.99)]'

Am I missing something? When I try paths without a predicate (like $.book[2].title) it works.

My full SQL query is (the JSON is in the content field):

SELECT Json_extract(content, '$.book[?(@.price == 22.99)]')
FROM json_test
WHERE id = 1

PS: I know I could use the WHERE statement for the query, but I would prefer to get it working using JSON path only.

2
  • 1
    See sqlite.org/json1.html#path_arguments for the syntax supported by json1 paths. Commented Feb 4, 2021 at 11:20
  • @Shawn Hmm, so do I understand it correctly that predicates are not supported? Only elements and indices? Shame. But thanks for the answer. Commented Feb 4, 2021 at 11:24

1 Answer 1

1

Something along the lines of:

SELECT j.value
FROM json_test AS jt
JOIN json_each(jt.content, '$.book') AS j
WHERE jt.id = 1 AND json_extract(j.value, '$.price') = 22.99

You can't avoid using a WHERE if you want to filter on a value in a json object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.