I am new to Postgres. I want to delete the JSON object from the JSON array.
I have a table where I am using jsonb column in that I am storing JSON array as like below.
[
{
"id": "c75e7a-001e-4d64-9613-62f666d42103",
"name": "Product1"
},
{
"id": "c75e7a-001e-4d64-9613-62f666d42103",
"name": null
},
{
"id": "c75e7a-001e-4d64-9613-62f666d42103",
"name": "Product2"
},
{
"id": "c75e7a-001e-4d64-9613-62f666d42103",
"name": null
}
]
I want to remove JSON objects from an array that contains a null value in the name key.
after removing answer should be like this
[
{
"id": "c75e7a-001e-4d64-9613-62f666d42103",
"name": "Product1"
},
{
"id": "c75e7a-001e-4d64-9613-62f666d42103",
"name": "Product2"
}
]
anyone, please help me to write the SQL query,
I know how to get all the records from the table which contains the null value.
SELECT *
FROM table_name
WHERE jsonb_col_name @>CAST('[{"name": null}]' AS JSONB);
But I don't know how to make a delete query please help me with this. How Can I do it using a query?