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.
  • 19,945
  • 7
  • 68
  • 85
singh
  • 21
  • 1

1 Answers1

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.

Kouber Saparev
  • 7,637
  • 2
  • 29
  • 26