1

I want to use where condition on a json object in a table, in postgreSql. how i need to do this for example: i have a table 'test' it has three columns name(varchar),url(varchar),more(json). i need to retrive date where css21Colors = Purple. more is a json type and below is the values of more field.

Please let me know what should be syntax of querying for the same?

more = {"colorTree":{"Purple":[{"Spanish Violet":"#522173"}],
"Brown":[{"Dark Puce":"#4e3347"}],"White":[{"White":"#ffffff"}],
"Black":[{"Eerie Black":"#1d0d27"}],"Gray":[{"Rose Quartz":"#a091a4"}]},
"sizeoutscount":0,"css21Colors":{"Purple":69,"Brown":5,"White":4,"Black":17,"Gray":3},
"sizeins": [],"sizeinscount":0,"sizeouts":[],"allsizes":["8","10","16"],
"css3Colors": {"Rose Quartz":3,"White":4,"Dark Puce":5,"Eerie Black":17,"Spanish
Violet":69},"hexColors":{"#522173":69,"#4e3347":5,"#ffffff":4,"#1d0d27":17,"#a091a4":3}}
Vivek S.
22.2k9 gold badges71 silver badges87 bronze badges
asked Apr 24, 2015 at 7:53
2
  • 1
    Mention PostgreSQL version (select version()) Commented Apr 24, 2015 at 8:26
  • PostgreSQL version is 9.4.0 Commented Apr 24, 2015 at 8:58

1 Answer 1

1
SELECT more->'css21Colors'->'Purple' FROM test;

Additionally you can query only the rows containing that key.

SELECT
 more->'css21Colors'->'Purple'
FROM
 test
WHERE
 (more->'css21Colors')::jsonb ? 'Purple';

Mind switching to the jsonb data type.

answered Apr 24, 2015 at 10:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.