Package - Writing Json object back to SQL fails

More
1 year 10 months ago #24265 by prashant
Hellos,

From URL logger , i recieve a json , which I rework with python , my json is inside formatted_json and I am now trying to write it back to SQL using packag but I keep getting erorr

I swear this was workign fine with 6.4.4.2

UPDATE event_log 
SET text_field_03 = 'formatted_json'
WHERE id = '00cf9c45-e7f7-41aa-99eb-7a21fad4115d';

OR


then i tried below too but no avail 

DECLARE @claude_data NVARCHAR(MAX);
SET @claude_data = JSON_MODIFY('{}', '$', JSON_QUERY(formatted_json));
UPDATE event_log 
SET text_field_03 = @claude_data
WHERE id = '00cf9c45-e7f7-41aa-99eb-7a21fad4115d';

OR
DECLARE @json_data NVARCHAR(MAX) = 'formatted_json';
UPDATE event_log 
SET text_field_03 = @json_data
WHERE id = '00cf9c45-e7f7-41aa-99eb-7a21fad4115d';
 

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #24266 by admin
it might fail if 'formatted_json' variable has some special characters inside for example a singe quote  

Mike
ETL Architect

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #24267 by prashant
Oh , the single quote definitely exists . To write to SQL, I need to encode / escape it with \ ? 

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #24268 by prashant
ChatGPT to rescue.

TO escape single quote ' , I did two single quotes ''
The change in my python package was

apps_json = apps_json.replace("'", "''")

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #24269 by admin
Good idea 

Mike
ETL Architect

Please Log in or Create an account to join the conversation.

Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Read more
Marketing
Set of techniques which have for object the commercial strategy and in particular the market study.
Google
Accept
Decline
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline
Google Analytics
Accept
Decline
Functional
Tools used to give you more features when navigating on the website, this can include social sharing.
Advertisement
If you accept, the ads on the page will be adapted to your preferences.
Google Ad
Accept
Decline
Save