title: Replace part of json (saved as varchar) using regex
body: <p>I am trying to replace some content inside JSON saved as VARCHAR in Postgres DB. I am using <code>REGEX_REPLACE</code> function for this. However, in some cases, it ignores my curly braces and produces invalid JSON.</p>
  <pre><code>CREATE TABLE expressions (
      id BIGINT,
      expression VARCHAR(512)
  );
  
  INSERT INTO expressions VALUES
      (1, '{params:[{&quot;value&quot;:&quot;test&quot;}]}'),
      (1, '{params:[{&quot;value&quot;:&quot;test1&quot;},{&quot;value&quot;:&quot;test2&quot;}]}');
      
  UPDATE expressions SET &quot;expression&quot; = REGEXP_REPLACE(&quot;expression&quot;,
        '&quot;value&quot;:&quot;(.*)&quot;','&quot;value&quot;:{&quot;value&quot;: &quot;\1&quot;}', 'g')
  WHERE true;
  
  SELECT * FROM expressions;
  </code></pre>
  <p>The first row produces correct JSON but the second one is missing one closing curcly bracket and produces this: <code>{params:[{&quot;value&quot;:{&quot;value&quot;: &quot;test1&quot;},{&quot;value&quot;:&quot;test2&quot;}}]}</code>.</p>
  <p>Does anyone know what is wrong with my expression?</p>
  <p>EDIT: I am trying to replace the following: <code>{&quot;param&quot;: [{&quot;value&quot;:&quot;x&quot;}]}</code> -&gt;  <code>{&quot;param&quot;: [{&quot;value&quot;:{value:&quot;x&quot;}}]}</code></p>
  <p>Basically, nest the <code>value</code> string into nested json with same structure. This needs to work for multiple values in the column so <code>{params:[{&quot;value&quot;:&quot;test1&quot;},{&quot;value&quot;:&quot;test2&quot;}]}</code> should produce <code>{params:[{&quot;value&quot;:{&quot;value&quot;:&quot;test1&quot;}},{&quot;value&quot;:{&quot;value&quot;:&quot;test2&quot;}}]}</code></p>