0

How to read JSON object matching query params? I want to filter data for video urls which only contains query params from metadata column and replace it by the removing the query params.

metadata
{"video-url":"xyz.com/video/xy4jnj?pubtool=oembed","provider":"some-video","video-id":"x8cse6q"}
{"video-url":"xyz.com/video/x8cse6q?pubtool=oembed","provider":"some-video","video-id":x8cse6q}
{"video-url":"xyz.com/video/x8cse6q","provider":"some-video","video-id":"x8cse6q"}
select * from content where metadata.video-url ilike %?pubtool%

Expected to return rows which consists of query param in the metadata column for the field video-url.

2
  • 1
    LIKE is used for text, not for JSON. Did you check the manual? postgresql.org/docs/current/functions-json.html Commented Nov 20, 2022 at 16:08
  • MySQL and PostgreSQL are two different products. Please keep only one tag for the DBMS. Commented Nov 20, 2022 at 17:09

2 Answers 2

1

You need irst to extract the vidourl from the json and compare it to your search pattern.

You need to convert the result to varchar to use like

SELECT
"metadata"
FROM video 
WHERE ("metadata"  #> '{"video-url"}')::varchar like '%?pubtool%'
metadata
{"video-url":"xyz.com/video/xy4jnj?pubtool=oembed","provider":"some-video","video-id":"x8cse6q"}
{"video-url":"xyz.com/video/x8cse6q?pubtool=oembed","provider":"some-video","video-id":"x8cse6q"}
SELECT 2

fiddle

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

Comments

1

Extracting the video-url from the metadata:

SELECT
  metadata->>'video-url' as "video-url"
FROM m;

see: DBFIDDLE

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.