modify sql_在SQL Server中使用JSON_MODIFY()修改JSON数据

本文详细介绍了如何使用SQL Server中的JSON_MODIFY()函数来修改JSON数据,包括更新属性值、添加新属性、删除属性、操作数组等。通过多个示例展示了JSON_MODIFY()的用法,帮助理解如何在SQL Server中灵活地处理JSON数据。
摘要由CSDN通过智能技术生成

modify sql

This article explores JSON_MODIFY() function to modify JSON Data in the SQL Server.

本文探讨了JSON_MODIFY()函数来修改SQL Server中的JSON数据。

介绍 (Introduction)

Java Script Object Notation is a popular language in major NoSQL databases and applications for mobile development. SQL Server 2016 introduced native support for JSON. Suppose you define a variable in SQL Server and it holds JSON key-value pairs.

Java脚本对象表示法是主要的NoSQL数据库和用于移动开发的应用程序中的流行语言。 SQL Server 2016引入了对JSON的本机支持。 假设您在SQL Server中定义了一个变量,并且该变量包含JSON键值对。

We use JSON_MODIFY() function to update the JSON string. It can update the following items:

我们使用JSON_MODIFY()函数更新JSON字符串。 它可以更新以下项目:

  • Update existing property value

    更新现有属性值
  • Add a new element in an existing array

    在现有数组中添加新元素
  • Delete a property from JSON string

    从JSON字符串中删除属性
  • Delete a property

    删除属性

JSON_MODIFY()函数的语法 (Syntax of JSON_MODIFY() function)

JSON_MODIFY (expression, path, newValue)

JSON_MODIFY(表达式,路径,newValue)

  • Expression: It is the JSON Data string that we want to update. It might be a variable or a column containing JSON 表达式 :这是我们要更新的JSON数据字符串。 它可能是变量或包含JSON的列
  • Path: We specify the property that requires an update in the JSON string. It requires the following arguments:

    路径:我们在JSON字符串中指定需要更新的属性。 它需要以下参数:

    [append] [lax | strict] $.<json path>

    [附加] [宽松| 严格] $。<json路径>

    • Append: It is an optional argument, and it specifies a new value that should be appended to the array 追加:这是一个可选参数,它指定了应该附加到数组的新值
    • Lax: it is the default mode in the path argument. Suppose we specify a property in the path argument that does not exist, in this case, the JSON_MODIFY function tries to insert the specified new value. It might give you an error message in case it cannot insert the value 宽松:这是path参数中的默认模式。 假设我们在path参数中指定了一个不存在的属性,在这种情况下,JSON_MODIFY函数尝试插入指定的新值。 万一无法插入值,可能会给您一条错误消息
    • Strict: In the strict mode, if the property we specified does not exist, it does not try to insert the value. You get an error message 严格:在严格模式下,如果我们指定的属性不存在,则它不会尝试插入该值。 您收到一条错误消息
    • Json_path: it contains the property path that we wish to update Json_path:它包含我们要更新的属性路径


  • New value: It is the new value that we require to update in the JSON

    新值:这是我们需要在JSON中更新的新值

Let’s understand the usage of the JSON_MODIFY() function using examples.

让我们使用示例来了解JSON_MODIFY()函数的用法。

示例1:更新JSON属性值 (Example 1: Update JSON property value)

In the below example, we have key-value pair for Brand and Product key. Suppose you wish to update the product value in this JSON, we can use the following code using JSON_MODIFY().

在以下示例中,我们具有“品牌”和“产品”键的键/值对。 假设您希望更新此JSON中的产品值,我们可以使用以下代码使用JSON_MODIFY()。

SELECT JSON_MODIFY('{"Brand":"HP","Product":"Laptop"}', '$.Product', 'Laptop') AS 'Updated JSON';

It returns the updated JSON string in the output.

它在输出中返回更新的JSON字符串。

Update JSON Data property value

In this example, note the following things.

在此示例中,请注意以下事项。

  • First argument expression contains original JSON {“Brand”:”HP”,”Product”:”Laptop”}
  • 第一个参数表达式包含原始JSON {“ Brand”:“ HP”,“ Product”:“ Laptop”}
  • $.Product is the property path that we want to update
  • $ .Product是我们要更新的属性路径
  • laptop is a new value that we want to update in the $.Product key 笔记本电脑是我们要在$ .Product键中更新的新值。

示例2:获取原始和更新的JSON数据 (Example 2: Get original and updated JSON data)

Suppose for comparison, we want both original (before the update) and updated (after update) JSON data in the output. In this query, we declared a variable and stored JSON into it. Later, we used JSON_MODIFY() function to get the updated JSON.

为了进行比较,我们希望输出中同时包含原始(更新之前)和更新(更新之后)JSON数据。 在此查询中,我们声明了一个变量并将JSON存储到其中。 稍后,我们使用JSON_MODIFY()函数获取更新的JSON。

DECLARE @OriginalJSON NVARCHAR(4000)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop"}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'$.Product', 'Laptop') AS 'Updated JSON';

In the output, we get both original and updated JSON.

在输出中,我们同时获取原始和更新的JSON。

Get original and updated JSON string

In the below query, note the following:

在下面的查询中,请注意以下几点:

  • Variable @path contains the required value for the update

    变量@path包含更新所需的值
  • Specify variable in the JSON_MODIFY() function

    在JSON_MODIFY()函数中指定变量
DECLARE @OriginalJSON NVARCHAR(4000), @newvalue varchar(30),@path varchar(20)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptp"}'
Set @newvalue='Laptop'
set @path='$.Product
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值