1

I have one table user with one JSON field user_data. Example these 3 lines :

{"name": "John", domain_names: ['john.com', 'lennon.com', 'john.fr']}
{"name": "Foo", domain_names: ['foo.com', 'bar.fr', 'bar.com']}
{"name": "Tirion", domain_names: ['tirion.co.uk']}

I want to get JSON lines with one or more .com domaine name (The first two lines). I tried this :

SELECT user_data 
FROM user 
WHERE json_array_elements(user_data)->>'domain_names' LIKE '%.com';

but it doesn't work... Do you know a way to get lines with .com domain ?

2
  • This would be extremely simple if you used a normalized data model. Commented Oct 10, 2016 at 14:28
  • Yes but original data are more complicated :) Commented Oct 10, 2016 at 14:39

1 Answer 1

2
SELECT user_data
FROM "user" u
WHERE EXISTS
         (SELECT 1
          FROM jsonb_array_elements_text(u.user_data->'domain_names') x(dom)
          WHERE dom LIKE '%.com');

By the way, you should avoid naming tables like reserved words (in that case user).

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.