title: How to use declared variable to create a json element
body: <p>I have a stored procedure, in this, I have declared a variable which holds a value from a select function. I need to use this value to create a json element, but it throws an exception</p>
  
  <pre><code>function jsonb_set(jsonb, unknown, character varying, boolean) does not exist
  </code></pre>
  
  <p>This is the function:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION test ( ) RETURNS 
  INTEGER AS $$
  DECLARE
  intent varchar;
  BEGIN  
  
  select id into intent from customer;
  UPDATE orders
  SET data = jsonb_set(
  data, 
  '{Items}',      -- the array in which we operate
  to_jsonb(
  (WITH ar AS(
    WITH temp AS(
      SELECT data-&gt;'Items' AS items   -- the array in which we operate
      FROM orders
      WHERE id = 1    -- the filtered order we are updating
    )
    SELECT jsonb_set(
      jsonb_array_elements(items),
      '{Quantity}',   -- the new field we are adding
      intent,          -- this is where i need to replace the variable
      true)
    FROM temp)
   SELECT (array_agg(ar.jsonb_set))
   FROM ar)),
   false)
    WHERE id = 1;
  return 0;
  
  EXCEPTION WHEN others THEN
  
  return 1;
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>Copying the snippet where I need to replace the variable:</p>
  
  <pre><code>SELECT jsonb_set(
      jsonb_array_elements(items),
      '{Quantity}',   -- the new field we are adding
      intent,          -- this is where i need to replace the variable
      true)
  </code></pre>