0

I have a table, in this table I have a JSON field. In this field I store arrays with some data. For example:

  1. ["a", "", "c"]
  2. ["", "", ""]
  3. ["a", "b", "c"]

I need to clear this field from empty values and get:

  1. ["a", "c"]
  2. null
  3. ["a", "b", "c"]

Then update the filed values.

11
  • REPLACE('[]',NULL,REPLACE('"", ','',columnJson)) like this? Commented Jan 28, 2020 at 9:47
  • I try it in this way: REPLACE('[]',NULL,REPLACE('"", ','','["a", "", "c"]')). Gives me syntax error Commented Jan 28, 2020 at 9:54
  • Share all the sentence, and gives us the syntax error- Commented Jan 28, 2020 at 10:02
  • [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('[]',NULL,REPLACE('"", ','','["a", "", "c"]'))' at line 1. The sentence is just: REPLACE('[]',NULL,REPLACE('"", ','','["a", "", "c"]')) Commented Jan 28, 2020 at 10:08
  • @pepe_botika69, What is your MySQL version? Commented Jan 28, 2020 at 10:17

1 Answer 1

1

Using REPLACE:

REPLACE(REPLACE(emails,'"", ',''), '[""]','null')

Working example here in SQLfiddle

@PaulSpiegel:
Doesn't work for ["", "b", ""]

Solved:

REPLACE(REPLACE(REPLACE(emails,'"", ',''), '[""]','null'),', ""','')
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't work for ["", "b", ""] - fiddle
That wasn't an option.
If you just want something, that only works for the given sample data, then you could just hardcode the result.

Your Answer

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.