You can use:
regexp_replace(text1, '("test":\s*")(([^"]|\\")*)(")', '\1' || 'other value' || '\4' , 'g')
But, warning! The regexp replaces inner ocurences of "test": value.
Notes:
- \" inside strings are Ok!
- there is no way of detect internals ocurences
- \1 & \4 are captures of the regexp, in this case you could put directly '"test": ' instead of '\1' and '"' instead of '\4'. But \n is more general
A complete example:
drop table if exists example;
create table example(json1 json, text1 text);
insert into example (json1, text1) values (
'{"in":{"test": "inner"}, "test": "outer", "other": "some"}',
'{"in":{"test": "inner"}, "test": "outer", "other": "some"}'
);
insert into example (json1, text1) values (
'{"in":{"test": "inner \"no\""}, "test": "outer \"yes\"", "other": "some"}',
'{"in":{"test": "inner \"no\""}, "test": "outer \"yes\"", "other": "some"}'
);
select * from example;
select regexp_replace(text1, '("test":\s*")(([^"]|\\")*)(")', '\1' || 'other value' || '\4' , 'g')
from example;
If you use postgres 9.4 you can use json or jsonb type for a column