SqlException in stored procedure of an Azure SQL database that goes away when updating stored procedure without any changes

Andreas Fandrich 20 Reputation points
2026-06-29T07:35:12.74+00:00

We have a stored procedure in an Azure SQL database which uses OPENJSON to access a json value. The field containing the value is checked with ISJSON to make sure that it really contains valid json. This stored procedure randomly throws an SqlException with this message:
JSON text is not properly formatted. Unexpected character '<' is found at position 0.

This error goes away when we just do an ALTER PROCEDURE without actually changing anything in the procedure. What can we do to fix this completely inexplicable behavior?

Azure SQL Database

Answer accepted by question author
Erland Sommarskog 137.5K Reputation points MVP Volunteer Moderator
2026-06-29T08:27:31.5266667+00:00

You are making the assumption that SQL Server would be intelligent enough to think "Ah, this call to ISJSON is there to check if the OPENJSON call will succeed. I better call ISJSON before I call OPENJSON". Certainly, not an unreasonable assumption, but, oh no. Rather SQL Server goes: "Hm, it seems more efficient to call OPENJSON on the spot and do that funny call to ISJSON later". In this age of AI, it is almost refreshing to hear about software that is just outright stupid.

When you reload the procedure, the current query plan is removed from the cache, and with the first value you use to call the procedure, SQL Server decides on a plan where it does thing in the correct order. But then some later event triggers recompilation, and at this point, the parameters inspires SQL Server to use the silly plan.

The solution is simple, change

OPENJSON(jsoncol)

to

OPENJSON (try_cast(jsoncol as json))

try_cast will return NULL for columns where the text is not json.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.