25

I have a task to remove a JSON property saved in a SQL Server database table's column. Here is the structure of my table

OrderId   Name   JSON

In the JSON column I have this JSON data:

{ 
    "Property1" :"",
    "Property2" :"",
    "metadata-department": "A",
    "metadata-group": "B"
}

I have over 500 records that have this json value.

Can I update all the records to remove metadata-department?

3 Answers 3

40

This question is old but I thought I'd post another solution for those who may end up here via search engine.

In SQL Server 2016 or SQL Azure, the following works:

DECLARE @info NVARCHAR(100) = '{"name":"John","skills":["C#","SQL"]}'

PRINT JSON_MODIFY(@info, '$.name', NULL)

-- Result: {"skills":["C#","SQL"]}

Source: https://msdn.microsoft.com/en-us/library/dn921892.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Worth mentioning that this approach only works if you're in lax mode (which is the default), if you're in strict mode then this will set the value of the node to be null instead of removing it.
0

In my SQL Server environment, I have installed CLR RegEx Replace functions and using these, I've achieved what you wanted:

DECLARE @Text NVARCHAR(MAX) = '{ 
    "Property1" :"",
    "Property2" :"",
    "metadata-department": "A",
    "metadata-group": "B"
}';

PRINT dbo.RegExReplace(@Text, '"metadata-department": "[A-Za-z0-z]",\r\n\s+', '');

Result:

{ 
    "Property1" :"",
    "Property2" :"",
    "metadata-group": "B"
}

CLR Source: https://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/

Comments

-1

In MYSQL the function is JSON_REMOVE():

UPDATE orders SET JSON=JSON_REMOVE(JSON, '$.metadata-department')

https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-remove

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.