title: Get one element from jsonb array in PostgreSQL
body: <p>I have the following schema in my PostgreSQL database:</p>
  
  <pre><code>CREATE TABLE survey_results (
      id integer NOT NULL
  );
  
  CREATE TABLE slide_results (
      id integer NOT NULL,
      survey_result_id integer,
      buttons jsonb DEFAULT '[]'::jsonb
  );
  
  INSERT INTO survey_results (id)
    VALUES (1);
  
  INSERT INTO slide_results (id, survey_result_id, buttons)
    VALUES (1, 1, '[{"text": "Not at all"}, {"text": "Yes"}]');
  
  INSERT INTO slide_results (id, survey_result_id, buttons)
    VALUES (2, 1, '[{"text": "No"}, {"text": "Yes"}]');
  </code></pre>
  
  <p>And the following query:</p>
  
  <pre><code>WITH data AS ( 
    select 
      sr.id ,
      jsonb_agg(row_to_json(slr)) AS json_row
    from slide_results slr
    INNER JOIN survey_results sr ON sr.id = slr.survey_result_id
    group by sr.id
  )
  
  SELECT id, json_row-&gt;0-&gt;&gt;'buttons' from data;
  </code></pre>
  
  <p>which returns:</p>
  
  <pre><code>| id  | ?column?                                  |
  | --- | ----------------------------------------- |
  | 1   | [{"text": "Not at all"}, {"text": "Yes"}] |
  </code></pre>
  
  <p>I want this query to return only the first element of buttons array. I tried something like this:</p>
  
  <pre><code>WITH data AS ( 
    select 
      sr.id ,
      jsonb_agg(row_to_json(slr)) AS json_row
    from slide_results slr
    INNER JOIN survey_results sr ON sr.id = slr.survey_result_id
    group by sr.id
  )
  
  SELECT id, json_row-&gt;0-&gt;&gt;'buttons'-&gt;&gt;1 from data;
  </code></pre>
  
  <p>but this returns me error:</p>
  
  <pre><code>Query Error: error: operator does not exist: text -&gt;&gt; integer
  </code></pre>
  
  <p>How can I fix that?</p>
  
  <p><a href="https://www.db-fiddle.com/f/gP761psywgmovfdTT7DjP4/1" rel="noreferrer">https://www.db-fiddle.com/f/gP761psywgmovfdTT7DjP4/1</a></p>