title: Using prepared parameters with jsonb functions in psycopg2
body: <p>I have this raw Postgres statement</p>
  
  <pre class="lang-sql prettyprint-override"><code>UPDATE objects 
  SET property=jsonb_set(property, '{path1,static}', '["a", "b", "c"]')
  WHERE id = 1;
  </code></pre>
  
  <p>which corresponds to this JSONB object in the db</p>
  
  <pre><code>{
    "property": {
      "path1": {"static": ["foo"]},
      "path2": {"static": ["bar"]}
    }
  }
  </code></pre>
  
  <p>The problem is I am using psycopg2 to execute this statement and want to build the path dynamically so that I can set all <code>path1, path2, path3, ...</code>. Obviously I can write it this way</p>
  
  <pre><code> def update(uid, path_key, values):
      """
      :params: int     path_key could be 1,2,3
      :params: list    values could be ["a", "b", "c"]
      """
      path = '{path{}, static}'.format(path_key)
      values = json.dumps(values)
      cursor.execute("UPDATE objects SET property=jsonb_set(property, %s, %s) WHERE id=%s;", [path, values, uid]
  </code></pre>
  
  <p>This works but I wonder if there is a way to leverage prepared statement to do it without me having to build the path or the value manually myself, e.g.</p>
  
  <pre><code>cursor.execute("""UPDATE objects
                    SET property=jsonb_set(property, '{%s,static}', %s) 
                    WHERE id=%s;""", [path, ("a", "b", "c"), uid])
  </code></pre>