I have a table with several fields that I want to compile in just one json field. The problem is there several null and emtpy string values in those columns and y don't want to put them in the json, just store the relevant values. I want to do this with a minimum of queries as possible.
Here is my current approach:
select
json_remove(
json_remove(json, json_search(json, 'one', 'null')),
json_search(json, 'all', '')
) result
from (
select
json_object(
'tag_1', coalesce(tag_1, 'null'),
'tag_2', coalesce(tag_2, 'null'),
'tag_3', coalesce(tag_3, 'null')
) json
from leads
) l2;
But the problem is the json_search output is incompatible with the json_remove input. ¿Any ideas?
Here's some example data:
-------------------------
| tag_1 | tag_2 | tag_3 |
-------------------------
| x | | null |
| | y | z |
-------------------------
And what I spect as a result:
--------------------------------
| result |
--------------------------------
| {'tag_1': 'x'} |
| {'tag_2': 'y', 'tag_3': 'z'} |
--------------------------------
Thanks.