Re: JSON_ARRAY_APPEND adding escape slash
I achieved a final solution.
Intermediate Solution.
-- SET @i = NULL;
SET @i = JSON_ARRAY(JSON_OBJECT('a', '1', 'b', '2'));
SET @j = '3';
SET @k = '4';
SET @a = IF( JSON_TYPE(@i) <=> 'ARRAY',
JSON_ARRAY_APPEND(@i, '$', JSON_OBJECT("a", @j, "b", @k)),
JSON_ARRAY_APPEND(JSON_ARRAY(), '$', JSON_OBJECT("a", @j, "b", @k))
);
SELECT @j, @a
It worked, but it limited the JSON object. This worked for my situation, but I wanted something more generic.
FINAL SOLUTION
SET @i = NULL;
-- SET @i = JSON_ARRAY(JSON_OBJECT('a', '1', 'b', '2'));
SET @j = '{"a": "3", "b": "4"}';
SET @a = IF( JSON_TYPE(@i) <=> 'ARRAY',
JSON_ARRAY_APPEND(@i, '$', CAST(@j AS JSON)),
JSON_ARRAY_APPEND(JSON_ARRAY(), '$', CAST(@j AS JSON))
);
SELECT @i, @j, @a
It means I can't create the JSON Object prior to passing to the function, but I can live with that.
I think I'm just too used to development software that would never change the value of something just because it thought it should.