First and foremost: I agree with both the comments of @a_horse_with_no_name and @dezso: you should normalize your data. JSON is not for that.
However, if some reason I cannot fathom really makes this an advantage, it is possible:
Create an expression based UNIQUE INDEX
:
CREATE UNIQUE INDEX people_data_pos_idx ON peoples( (data->>'pos') ) ;
If, at this point, you try to insert the following piece of data into your table (with an already existing ->>pos):
INSERT INTO peoples(data)
VALUES
('{"name": "Eve", "pos":"DBA", "age":34}') ;
You get this as a response:
ERROR: duplicate key value violates unique constraint "people_data_pos_idx"
SQL state: 23505
Detail: Key ((data ->> 'pos'::text))=(DBA) already exists.
NOTE: I've assumed that data.pos
will always be a string. If you want to generalize, you can use ( (data->'pos') )
instead. You would index then a JSON(B) expression instead of a text. Check JSON Functions and Operators.