2

I want to search for specific values in a json object, but can't use json_textcontains.

Table:

|   ID   |    NAME   |                    JSONCOLUMN               | ...
|   1    |    test   |{"key1" : "24", "key2": "35", "key3" : "57"} | ...
|   2    |    test2  |{"key1" : "67", "key2": "78", "key3" : "31"} | ...
|   3    |    test3  |{"key1" : "12", "key2": "35", "key3" : "99"} | ...

Now I have a search term e.g. "35" and want to get all rows that contain this term in the jsoncolumn as value (I don't want to search the keys).

So in the example above rows 1 and 3.

I can achieve that with folowing query:

select * from T t where JSON_TEXTCONTAINS(t.jsoncolumn, '$', '35')

But I cannot use json_textcontains. So I'm looking for an equivalent query.

Edit: Clarified what I want to get from the query.

2
  • 2
    But... json_textcontains doesn't produce any output, it is just a condition. It doesn't extract any information from the JSON data. Commented Aug 16, 2022 at 13:29
  • Yeah, and the question is, is there another sql condition, which can search in json data. The above sql statement gives me the rows i want, but i can't use the json_textcontains in my query. So an equivalent replacement of the json_textcontains in the where clause. Commented Aug 16, 2022 at 13:34

1 Answer 1

1

You can use a LIKE condition or the INSTR() function for the matching.

However, just using this on t.jsoncolumn would find matches in the full json, i.e. also in the key part. To just search in the values, you could extract them first using JSON_QUERY.

Both

SELECT * 
  FROM t 
 WHERE JSON_QUERY(jsoncolumn, '$.*' WITH WRAPPER) LIKE '%35%'

and

SELECT * 
  FROM t
 WHERE INSTR(JSON_QUERY(t.jsoncolumn, '$.*' WITH WRAPPER), '35') > 0;

should produce the same result as your JSON_TEXTCONTAINS() example.

See this db<>fiddle.

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

6 Comments

I think we tried to use 'like' already, but this includes the keys and there is overlapping naming between keys and values, so i get rows where the key matches but not the value. Or am I wrong here?
No, you are right, this answer will also match in those cases, whereas json_textcontains will not. Before your edit, I did not get the question right.
Ok, yeah i tried to clarify it in the edit, sorry. Do you think what I'm looking for is possible?
You could use JSON_QUERY. I've updated my answer.
Ok, i got it to work with a few changes, i needed to use regexp_like, because i have many different conditions to match not just numbers like in my example. But your answer helped me. Thanks!
|

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.