All I try to get is a simple SQL statement to build:
{\"status\":{\"code\":404,\"message\":\"Not found\"},\"otherthing\":20}
If I set as :
It's not exact answer to your question, but I hope it will give solution to your problem.
You can construct expected output without nested query, just define hierarchy using property names, like this:
DECLARE @ReturnJSON nvarchar(max)
SET @ReturnJSON = (
SELECT
404 as [status.code]
,'Not found' as [status.message]
, 20 as [otherthing]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER) ;
SELECT @ReturnJSON