title: Delete element in a deeply nested array in jsonb column - Postgres
body: <p>I have a table <code>my_table</code> with a jsonb column that contains some data, for instance, in a single row, the column can contain the following data: </p>
  
  <pre><code>[
    {
      "x_id": "1",
      "type": "t1",
      "parts": [
         { "part_id": "1", price: 400 },
         { "part_id": "2", price: 500 },
         { "part_id": "3", price: 0 }
       ]
    },
    {
      "x_id": "2",
      "type": "t1",
      "parts": [
         { "part_id": "1", price: 1000 },
         { "part_id": "3", price: 60 }
       ]
    },
    {
      "x_id": "3",
      "type": "t2",
      "parts": [
         { "part_id": "1", price: 100 },
         { "part_id": "3", price: 780 },
         { "part_id": "2", price: 990 }
       ]
    }
  ]
  
  </code></pre>
  
  <p>I need help finding how to delete an element from the <code>parts</code> array given a <code>x_id</code> and a <code>part_id</code>.</p>
  
  <p><strong>Example</strong></p>
  
  <p>given <code>x_id=2</code> and <code>part_id=1</code>, I need the data to be updated to become:</p>
  
  <pre><code>[
    {
      "x_id": "1",
      "type": "t1",
      "parts": [
         { "part_id": "1", price: 400 },
         { "part_id": "2", price: 500 },
         { "part_id": "3", price: 0 }
       ]
    },
    {
      "x_id": "2",
      "type": "t1",
      "parts": [
         { "part_id": "3", price: 60 }
       ]
    },
    {
      "x_id": "3",
      "type": "t2",
      "parts": [
         { "part_id": "1", price: 100 },
         { "part_id": "3", price: 780 },
         { "part_id": "2", price: 990 }
       ]
    }
  ]
  </code></pre>
  
  <p>PS1: these data cannot be normalized, so that's not a possible solution. </p>
  
  <p>PS2: I'm running PostgreSQL 9.6</p>
  
  <p>PS3: I  have checked <a href="https://stackoverflow.com/questions/40601616/how-to-delete-array-element-in-jsonb-column-based-on-nested-key-value">this question</a> and <a href="https://stackoverflow.com/questions/30005584/postgres-remove-element-from-jsonb-array?rq=1">this question</a> but my data structure seems too complex compared to the other questions thus I can't apply the given answers.</p>
  
  <p><strong>Edit1</strong>: the json data can be big, especially the <code>parts</code> array, which can have from as few as 0 element to thousands.</p>