Re: JSON_ARRAY_APPEND adding escape slash
Posted by: Derek McKinnon
Date: August 21, 2022 11:37PM

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.

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: JSON_ARRAY_APPEND adding escape slash
35
August 21, 2022 11:37PM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.