JSON text is not properly formatted. Unexpected character 'N' is found at position 0 - json

I am new to JSON in SQL. I am getting the error "JSON text is not properly formatted. Unexpected character 'N' is found at position 0." while executing the below -
DECLARE #json1 NVARCHAR(4000)
set #json1 = N'{"name":[{"FirstName":"John","LastName":"Doe"}], "age":31, "city":"New York"}'
DECLARE #v NVARCHAR(4000)
set #v = CONCAT('N''',(SELECT value FROM OPENJSON(#json1, '$.name')),'''')
--select #v as 'v'
SELECT JSON_VALUE(#v,'$.FirstName')
the " select #v as 'v' " gives me
N'{"FirstName":"John","LastName":"Doe"}'
But, using it in the last select statement gives me error.
DECLARE #v1 NVARCHAR(4000)
set #v1 = N'{"FirstName":"John","LastName":"Doe"}'
SELECT JSON_VALUE(#v1,'$.FirstName') as 'FirstName'
also works fine.

If you're using SQL Server 2016 or later there is build-in function ISJSON which validates that the string in the column is valid json.
Therefore you can do things like this:
SELECT
Name,
JSON_VALUE(jsonCol, '$.info.address.PostCode') AS PostCode
FROM People
WHERE ISJSON(jsonCol) > 0

You are adding the Ncharacter in your CONCAT statement.
Try changing the line:
set #v = CONCAT('N''',(SELECT value FROM OPENJSON(#json1, '$.name')),'''')
to:
set #v = CONCAT('''',(SELECT value FROM OPENJSON(#json1, '$.name')),'''')

JSON_VALUE function may first be executed on all rows before applying the where clauses. it will depend on execution plan so small things like having top clause or ordering may have a impact on that.
It means that if your json data is invalid anywhere in that column(in the whole table), it will throw an error when the query is executed.
So find and fix those invalid json formats first. for example if that column has a ' instead of " it cannot be parsed and will cause the whole TSQL query to throw an error

Related

T-SQL JSON_MODIFY Error: Argument data type uniqueidentifier (datetime, money etc) is invalid

JSON_MODIFY does not support many base types in the 3rd argument. How fix it?
DECLARE #info NVARCHAR(200)='{"name":"John","skills":["C#","SQL"]}'
SELECT JSON_MODIFY(#info, '$.code', NEWID())
SELECT JSON_MODIFY(#info, '$.date', GETDATE())
SELECT JSON_MODIFY(#info, '$.cost', CAST(1 AS money))
Msg 8116, Level 16, State 1, Line 3
Argument data type uniqueidentifier is invalid for argument 3 of
json_modify function.
Msg 8116, Level 16, State 1, Line 5
Argument data type datetime is invalid for argument 3 of json_modify
function.
Msg 8116, Level 16, State 1, Line 8
Argument data type money is invalid for argument 3 of json_modify
function.
Try use my dbo.JSON_MODIFY function that solved problem
CREATE FUNCTION dbo.JSON_MODIFY(#expression nvarchar(max), #path nvarchar(max), #newValue sql_variant)
RETURNS nvarchar(max)
AS
BEGIN
DECLARE #tempJson nvarchar(max) = (SELECT #newValue AS col1 FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
RETURN JSON_MODIFY(#expression, #path, JSON_VALUE(#tempJson, '$.col1'))
END
GO
DECLARE #info NVARCHAR(100)='{"name":"John","skills":["C#","SQL"]}'
PRINT dbo.JSON_MODIFY(#info, '$.code', NEWID())
PRINT dbo.JSON_MODIFY(#info, '$.date', GETDATE())
PRINT dbo.JSON_MODIFY(#info, '$.cost', CAST(1 AS money))
GO
Output results:
{"name":"John","skills":["C#","SQL"],"code":"85543240-38C5-4647-B555-4388ADAD71F0"}
{"name":"John","skills":["C#","SQL"],"date":"2019-10-03T16:16:10.600"}
{"name":"John","skills":["C#","SQL"],"cost":"1.0000"}
I can think of a few reasons why updating JSON in SQL is a bad idea starting with the configuration options of those that consume the json. If you use need to be able to round-trip the JSON and you use something like NewtonSoft default settings you would need to cast/ format a date string in a particular way
select format(GetUtcDate(),'yyyy-MM-dd''T''HH:mm:ss.fff')+'Z'
in your sample, you should probably use if you use .net to read the data when it leaves your database... in any way you need to make sure you align your database with those that consume the data and, in my use-case use format to make sure the data does not generate corrupt JSON.
DECLARE #info NVARCHAR(100)='{"name":"John","skills":["C#","SQL"]}'
in your modify this would look like this
SELECT JSON_MODIFY(#info, '$.date', format(GetUtcDate(),'yyyy-MM-dd''T''HH:mm:ss.fff')+'Z')

MySQL after update trigger using CONCAT_WS: Why is it inserting a newline where I DON'T want it?

The intended result is to store the notes of edits to a field, in another field.
I want the new notes to APPEND to the storage field, and since the is not function that does this I am attmpting to find a way to work this out without adding more layers of code like functions and stored procedures.
/* Before Update Trigger */
DECLARE v_description VARCHAR(255);
DECLARE v_permnotes MEDIUMTEXT;
DECLARE v_oldnote VARCHAR(500);
DECLARE v_now VARCHAR(25);
SET v_now = TRIM(DATE_FORMAT(NOW(), '%Y-%m-%d %k:%i:%s'));
SET v_oldnote = OLD.notes;
IF (NEW.permanent_notes IS NULL) THEN
SET v_permnotes = '';
ELSE
SET v_permnotes = OLD.permanent_notes;
END IF;
SET NEW.permanent_notes = CONCAT_WS(CHAR(10), v_permnotes, v_now,": ", v_description);
I'm aiming to have the results in the permanent field look like this
<datetime value>: Some annotation from the notes field.
<a different datetime>: A new annotation
etc....
What I get from my current trigger:
2018-12-30 17:15:50
:
Test 17: Start from scratch.
2018-12-30 17:35:51
:
Test 18: Used DATE_FORMAT to sxet the time
2018-12-30 17:45:52
:
Test 19. Still doing a carriage return after date and after ':'
I can't figure out why there is a newline after the date, and then again after the ':'.
If I leave out CHAR(10), I get:
Test 17: Start from scratch.
2018-12-30 17:35:51
:
Test 18: Used DATE_FORMAT to sxet the time
2018-12-30 17:45:52
:
Test 19. Still doing a carriage return after date and after ':'Test 20. Still doing a carriage return after date and after ':'
Some fresh/more experienced eyes would be really helpful in debugging this.
Thanks.
I think you should just be using plain CONCAT here:
DECLARE separator VARCHAR(1);
IF (NEW.permanent_notes IS NULL) THEN
SET separator = '';
ELSE
SET separator = CHAR(10)
END IF;
-- the rest of your code as is
SET
NEW.permanent_notes = CONCAT(v_permnotes, separator, v_now, ": ", v_description);
The logic here is that we conditionally print a newline (CHAR(10)) before each new log line, so long as that line is not the very first. You don't really want CONCAT_WS here, which is mainly for adding a separator in between multiple terms. You only want a single newline in between each logging statement.

unescape diactrics in \u0 format (json) in ms sql (SQL Server)

I'm getting json file, which I load to Azure SQL databese. This json is direct output from API, so there is nothing I can do with it before loading to DB.
In that file, all Polish diactircs are escaped to "C/C++/Java source code" (based on: http://www.fileformat.info/info/unicode/char/0142/index.htm
So for example:
ł is \u0142
I was trying to find some method to convert (unescape) those to proper Polish letters.
In worse case scenario, I can write function which will replace all combinations
Repalce(Replace(Replace(string,'\u0142',N'ł'),'\u0144',N'ń')))
And so on, making one big, terrible function...
I was looking for some ready functions like there is for URLdecode, which was answered here on stack in many topics, and here: https://www.codeproject.com/Articles/1005508/URL-Decode-in-T-SQL
Using this solution would be possible but I cannot figure out cast/convert with proper collation and types in there, to get result I'm looking for.
So if anyone knows/has function that would make conversion in string for unescaping that \u this would be great, but I will manage to write something on my own if I would get right conversion. For example I tried:
select convert(nvarchar(1), convert(varbinary, 0x0142, 1))
I made assumption that changing \u to 0x will be the answer but it gives some Chinese characters. So this is wrong direction...
Edit:
After googling more I found exactly same question here on stack from #Pasetchnik: Json escape unicode in SQL Server
And it looks this would be the best solution that there is in MS SQL.
Onlty thing I needed to change was using NVARCHAR instead of VARCHAR that is in linked solution:
CREATE FUNCTION dbo.Json_Unicode_Decode(#escapedString nVARCHAR(MAX))
RETURNS nVARCHAR(MAX)
AS
BEGIN
DECLARE #pos INT = 0,
#char nvarCHAR,
#escapeLen TINYINT = 2,
#hexDigits TINYINT = 4
SET #pos = CHARINDEX('\u', #escapedString, #pos)
WHILE #pos > 0
BEGIN
SET #char = NCHAR(CONVERT(varbinary(8), '0x' + SUBSTRING(#escapedString, #pos + #escapeLen, #hexDigits), 1))
SET #escapedString = STUFF(#escapedString, #pos, #escapeLen + #hexDigits, #char)
SET #pos = CHARINDEX('\u', #escapedString, #pos)
END
RETURN #escapedString
END
Instead of nested REPLACE you could use:
DECLARE #string NVARCHAR(MAX)= N'\u0142 \u0144\u0142';
SELECT #string = REPLACE(#string,u, ch)
FROM (VALUES ('\u0142',N'ł'),('\u0144', N'ń')) s(u, ch);
SELECT #string;
DBFiddle Demo

The argument 1 of the XML data type method "modify" must be a string literal

Hi i am getting the string literal error when i am trying to add an attribute to the child node. How can i modify my code in order to add an attribute successfully.
declare #count int=(select mxGraphXML.value('count(/mxGraphModel/root/Cell/#Value )','nvarchar') from TABLE_LIST
where Table_ListID=1234 )
declare #index int=1;
while #index<=#count
begin
declare #Value varchar(100)= #graphxml.value('(/mxGraphModel/root/Cell/#Value )[1]','nvarchar');
SET #graphxml.modify('insert attribute copyValueID {sql:variable("#Value ")}
as first into (/mxGraphModel/root/Cell)['+convert(varchar,#index)+']');
end
set #index=#index+1;
end
You're using the addition operator where you should be using the CONCAT function. So
'insert attribute copyValueID {sql:variable("#Value ")}
as first into (/mxGraphModel/root/Cell)['+convert(varchar,#index)+']'
is being coerced into a number. Try:
CONCAT('insert attribute copyValueID {sql:variable("#Value ")}
as first into (/mxGraphModel/root/Cell)[',convert(varchar,#index),']')
instead.
Adam, you can do it in Microsoft T-SQL like this:
declare #sql nvarchar(max)
set #sql = 'set #myxml.modify(''
insert (
attribute scalableFieldId {sql:variable("#sf_id")},
attribute myTypeId {sql:variable("#my_type_id")}
) into (/VB/Condition/Field[#fieldId=sql:variable("#field_id")
and #fieldCode=sql:variable("#field_code")])['+
cast(#instance as varchar(3))+']'')'
exec sp_executesql
#sql
,N'#myxml xml output, #field_code varchar(20),
#field_id varchar(20), #sf_id int, #my_type_id tinyint'
,#myxml = #myxml output
,#field_code = #field_code
,#field_id = #field_id
,#sf_id = #sf_id
,#my_type_id = #my_type_id
See what I've done here? It's just a clever usage of Dynamic SQL to overcome Microsoft's moronic limitation of "string literal error".
IMPORTANT NOTE: yes, you can MOSTLY do this by using sql:variable() in SOME places BUT good luck trying to use it in the node number qualifier inside the square brackets! You can't do this without Dynamic SQL by design!
The trick is not mine actually, I got the idea from https://www.opinionatedgeek.com/Snaplets/Blog/Form/Item/000299/Read after banging my head against the wall for a while.
Feel free to ask questions if my sample does not work or something is not clear.

MySQL Error pls-00103

I am making a MySQLand getting an error I dont know how to fix it. What the function should be doing is asking the user for input, this input should be stored in the corresponding variables and return a variable. But when i try run the function I am getting errors on the lines that store the variables. I am quite new to mySQL so i might be doing something stupid. Any help will be great thank you
This is is the mysql function
create or replace FUNCTION AD_AGENCY_INFO(agency_id in agency_id%type)
RETURN agency_id
DECLARE
v_no_of_runs AD_AGENCY.NO_OF_AD_RUNS%TYPE = '&enter_number_of_Runs';
v_credit_worthy AD_AGENCY.CREDIT_WORTHY%TYPE = '&enter_credit_worthy';
v_available_slots AD_AGENCY.AVAILABLE_SLOTS%TYPE = '&enter_available_slots';
v_status AD_AGENCY.STATUS%TYPE = '&enter_status';
BEGIN
insert into ad_agency values (agency_id, v_no_of_runs, v_credit_worthy,v_available_slots, v_status);
insert into ad (agency_id) values (agency_id);
commit;
RETURN (agency_id));
END AD_AGENCY_INFO;
The error that i am getting is the following and is the same for lines 7,8,9
Error(6,45): PLS-00103: Encountered the symbol "=" when expecting one of the following: := ( ; not null range default character The symbol ":= was inserted before "=" to continue.
Try changing your declare statement(s) to be
v_no_of_runs AD_AGENCY.NO_OF_AD_RUNS%TYPE := '&enter_number_of_Runs'