1

I have data in database from tabel data_for_filter and column src_data (type: JSON) like this

{"filter": {"name": "Anton", "activity": "Studying"},"primary": true, "position": 1}

in postgresql, how I can do search query with where clause name like '%Here%' ?

i was try this query

select src_data from data_for_filter where src_data ->> 'filter' #>> 'name' like '%Here%'

but it raised error

2 Answers 2

1

->> returns a text, so you can't apply #>> on that. -> returns a proper jsonb (or json) value. The #>> operator however requires an array, so #>> 'name' would lead to the next error

where src_data -> 'filter' ->> 'name' like '%Here%'

you can simplify this using:

where src_data #>> '{filter,name}' like '%Here%'
Sign up to request clarification or add additional context in comments.

Comments

1

Unless I’m misunderstanding, you’re wanting to get the JSON object field by the key “filter”, so you’re using the incorrect operator. Try -> ‘filter’

Here’s a reference:

https://www.postgresql.org/docs/current/functions-json.html

2 Comments

Im new on database with json data type .. hehe btw thankyou for reference.
We're all new to something or we're doing it wrong! Enjoy the journey!

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.