Skip to content
Advertisement

How to check json format in SQL?

I have a table with a column of type Nvarchar where json is stored. But the format of some of its columns is wrong. So I get an error when I parse them all.

I want my query to ignore lines that have errors.

It gives this error when I run on all lines:

JSON text is not properly formatted. Unexpected character ‘.’ is found at position 0.

, but it executes correctly on the top 10.

Advertisement

Answer

If I follow you correctly, you could use ISJSON()to check the validity of body before passing it to OPENJSON():

This filters out rows whose content is invalid. If you want to retain them, and return a null result instead, then:

And, of course, you can identify the offending rows like so:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement