In SQL Server 2008 (dataset from SQL Azure) I have a query where I am attempting to parse multiple elements from an existing JSON formatted column.
JSON_VALUE with the replace method worked fine for the first few columns.
However, SQL Server is throwing an error now after adding an additional JSON_VALUE using the replace method.
This is my query:
SELECT
ApplicationID, Type, CreateDate,
JSON_VALUE(RawContent,'$.errorName') AS ErrorName,
JSON_VALUE(replace(replace(RawContent,'[',''),']',''),'$.errorMessage') AS ErrorMessage,
JSON_VALUE(RawContent,'$.bookId') AS BookId,
JSON_QUERY(RawContent,'$.urlParams') AS UrlParams,
JSON_VALUE(replace(RawContent,',',''),'$.userAgent') AS UserAgent, --line in question
JSON_VALUE(replace(RawContent,' ','') ,'$.stackTrace') AS StackTrace
FROM
dbo.Feedback
WHERE
ISJSON(RawContent) > 0
ORDER BY
RecordID
My json formatted column (RawContent) looks like this:
{"errorName": "not authorized",
"errorMessage":"Request denied: account permission error",
"bookId":"150675",
"urlParams":{"audiobook":"https://node.axisnow.com/#audiobook/"},
"userAgent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"}
So I'm expecting the query to produce a column for each JSON_VALUE, while removing the comma ',' from within the column "userAgent" as just:
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
only without the comma "," to look like:(KHTML like Gecko)
The replace method works for errormessage, and stack trace, but for some reason it causes an error when I run it on the userAgent line, it seems to not like the "," character and I was wondering what I need to use as I can't find anything online.
Error msg:
Msg 13609, Level 16, State 2, Line 1
JSON text is not properly formatted. Unexpected character ':' is found at position 43.
Please help. My boss was expecting this to be done yesterday...
SELECT @@VERSIONand see what engine version your coding against. Again: SQL Server 2008 never had anything likeJSON_VALUE- this cannot possibly work on 2008 (unless you've installed some kind of a .NET / SQL-CLR based extension or something).