1

I have save data in PostgreSQL as given below:

{"tags": "Tag 1,Tag 2,Tag 3"}
{"tags": "Tag 1,Tag 4,Tag 5"}
{"tags": "Tag 6,Tag 1,Tag 2"}

I want search records where 'Tag 2' or Tag 3 exists?

Table schema, create procedure is as below,

--create table 
CREATE TABLE "tblIrsInputTagging" ( 
  "IrsInputTaggId" serial NOT NULL, 
  "Irs_tags" json NOT NULL, CONSTRAINT "tblIrsInputTagging_pkey" 
  PRIMARY KEY ("IrsInputTaggId") 
) WITH ( OIDS=FALSE ); 
ALTER TABLE "tblIrsInputTagging" OWNER TO "postgre";

--insert json record 

INSERT INTO "tblIrsInputTagging" ("Irs_tags") 
VALUES ( '{"tags": "Tag 1,Tag 2,Tag 3"}' );

INSERT INTO "tblIrsInputTagging" ("Irs_tags") 
VALUES ( '{"tags": "Tag 1,Tag 4,Tag 5"}' ); 

INSERT INTO "tblIrsInputTagging" ("Irs_tags") 
VALUES ( '{"tags": "Tag 6,Tag 1,Tag 2"}' );
3
  • data type of column is json Commented May 28, 2017 at 4:20
  • are those taglists, i mean, "Tag1, Tag2, Tag3" an array, or just a text? Commented May 28, 2017 at 4:26
  • its just a text Commented May 28, 2017 at 5:57

2 Answers 2

1

i don't think there is some specific function to check per json items. But you can do so by casting the data type as text then check it with like or ilike operator.

SELECT col1, col2 FROM table_name WHERE CAST(col1 AS text) ilike '%value%'

notice that the col1 column is the json data type.

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

Comments

0

Let's say the column name is tags. In that case, you can query like below.
If there is something specific, let me know, so I might need to update below snippet.

SELECT col_1, col_2, .. col_n 
FROM your_table 
WHERE tags::json->'tags' ILIKE '%Tag2%'
OR
tags::json->'tags' ILIKE '%Tag3%'

7 Comments

Looks like your LIKE pattern is missing wildcards.
ERROR: column "Tag2" does not exist LINE 2: WHERE "Irs_tags"::json->'tags' ILIKE "Tag2" ^ ********** Error ********** ERROR: column "Tag2" does not exist SQL state: 42703 Character: 73
Dear Laurenz Albe its showing error that column "Tag 2" doses not exist even its not a column it is data inside the tag column of jason.
@NarenderPal please update your question body with that error message.
ERROR: column "%Tag2%" does not exist still same error
|

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.