I have found a workaround to this bug
I kept chipping away at this issue and JSON_MODIFY bug is only happening when an object has sole property that is being removed, for example
"editorOptions": {
"placeholder": "Directory"
},
but when there are other properties too, then JSON_MODIFY works
This can be taken advantage of by checking if there are other properties besides one being removed and if not, then the object itself is removed
Therefore, my workaround became this
DECLARE @key_content int = 2
--SET @key_content = 3 -- removes property too
IF NOT EXISTS -- placeholder is a single object property
(
SELECT *
FROM OPENJSON(@j, CONCAT('lax $.content', QUOTENAME(@key_content), '.editorOptions'))
WHERE [key] != 'placeholder'
)
BEGIN
SET @j = JSON_MODIFY
(
@j
,CONCAT('lax $.content', QUOTENAME(@key_content), '.editorOptions')
,NULL -- remove object
)
END
ELSE
BEGIN
SET @j = JSON_MODIFY
(
@j
,CONCAT('lax $.content', QUOTENAME(@key_content), '.editorOptions.placeholder')
,NULL -- remove property
)
END
SELECT @j as [@j], JSON_VALUE(@j, CONCAT('lax $.content', QUOTENAME(@key_content), '.editorOptions.placeholder')) as placeholder
This worked for me.
when @Key _content = 3, then just property ["placeholder": "Version"] is removed.
Maybe this can help someone else with the same issue
Thanks
Vladimir