0

I'm using Mysql 8. I want to remove the specific object from JSON Array which has value like field_1568988989723 in Mysql JSON column

Here is my JSON

[{"section": {"name": "Basic Info", "order": 0, "fields": [{"name": "field_1568988989723", "order": 0}, {"name": "field_1568989125942", "order": 1}]}}]

I've used this way

UPDATE apps_object
 SET detail_view = JSON_REMOVE( 
 detail_view, REPLACE( 
 JSON_SEARCH( detail_view, 'all', 'field_1568992047479', null, '$**.fields' )
 , '"'
 , ''
 ) 
 ) where id = 34;

But it just removes

"value":"field_1568988989723"

but not he whole object. Is there any way to remove object in MySql not in Programing code?

asked Sep 27, 2019 at 15:53
2
  • Try dbfiddle. Commented Sep 29, 2019 at 8:52
  • Thank you @wchiquito. It worked. And now my query is UPDATE apps_object SET detail_view = IFNULL( JSON_REMOVE( detail_view, JSON_UNQUOTE( REPLACE( JSON_SEARCH(detail_view, 'all', 'field_1568988989723', NULL, '$**.fields'), '.name', '' ) ) ), detail_view ) where id = 34; Commented Oct 1, 2019 at 12:18

1 Answer 1

2

I'm thankful to @wchiquito for solving my problem

[{"section": {"name": "Basic Info", "order": 0, "fields": [{"name": "field_1568988989723", "order": 0}, {"name": "field_1568989125942", "order": 1}]}}]

This is the query which solved my problem

UPDATE apps_object SET detail_view = IFNULL( JSON_REMOVE( detail_view, JSON_UNQUOTE( REPLACE( JSON_SEARCH(detail_view, 'all', 'field_1568988989723', NULL, '$**.fields'), '.name', '' ) ) ), detail_view ) where id = 34;

With the help of dbfiddle

answered Oct 1, 2019 at 12:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.