SQL Server JSON 数据操作

使用内置函数 (SQL Server) 验证、查询和更改 JSON 数据 
使用 OPENJSON 分析和转换 JSON 数据 (SQL Server)   

1、JSON 内置函数
ISJSON 测试字符串是否包含有效 JSON。
JSON_VALUE 从 JSON 字符串中提取标量值。
JSON_QUERY 从 JSON 字符串中提取对象或数组。
JSON_MODIFY 更新 JSON 字符串中属性的值,并返回已更新的 JSON 字符串。

2、示例 JSON 文本

{
  "id": "WakefieldFamily",
  "parents": [
    {
      "familyName": "Wakefield",
      "givenName": "Robin"
    },
    {
      "familyName": "Miller",
      "givenName": "Ben"
    }
  ],
  "children": [
    {
      "familyName": "Merriam",
      "givenName": "Jesse",
      "gender": "female",
      "grade": 1,
      "pets": [
        {
          "givenName": "Goofy"
        },
        {
          "givenName": "Shadow"
        }
      ]
    },
    {
      "familyName": "Miller",
      "givenName": "Lisa",
      "gender": "female",
      "grade": 8
    }
  ],
  "address": {
    "state": "NY",
    "county": "Manhattan",
    "city": "NY"
  },
  "creationDate": 1431620462,
  "isRegistered": false
}

3、新建表,插入测试数据

CREATE TABLE Families (
   id int identity constraint PK_JSON_ID primary key,
   doc nvarchar(max)
)

INSERT INTO [dbo].[Families] ([doc]) VALUES ('{"id":"WakefieldFamily","parents":[{"familyName":"Wakefield","givenName":"Robin"},{"familyName":"Miller","givenName":"Ben"}],"children":[{"familyName":"Merriam","givenName":"Jesse","gender":"female","grade":1,"pets":[{"givenName":"Goofy"},{"givenName":"Shadow"}]},{"familyName":"Miller","givenName":"Lisa","gender":"female","grade":8}],"address":{"state":"NY","county":"Manhattan","city":"NY"},"creationDate":1431620462,"isRegistered":false}')
GO

INSERT INTO [dbo].[Families] ([doc]) VALUES ('{"id":"WakefieldFamily","parents":[{"familyName":"Wakefield","givenName":"Robin"},{"familyName":"Miller","givenName":"Ben"}],"children":[{"familyName":"Merriam","givenName":"Jesse","gender":"female","grade":1,"pets":[{"givenName":"Goofy"},{"givenName":"Shadow"}]},{"familyName":"Miller","givenName":"Lisa","gender":"female","grade":8}],"address":{"state":"NY","county":"Manhattan","city":"NY"},"creationDate":1431620462,"isRegistered":false}')
GO

INSERT INTO [dbo].[Families] ([doc]) VALUES ('{"id":"WakefieldFamily","parents":[{"familyName":"Wakefield","givenName":"Robin"},{"familyName":"Miller","givenName":"Ben"}],"children":[{"familyName":"Merriam","givenName":"Jesse","gender":"female","grade":1,"pets":[{"givenName":"Goofy"},{"givenName":"Shadow"}]},{"familyName":"Miller","givenName":"Lisa","gender":"female","grade":8}],"address":{"state":"NY","county":"Manhattan","city":"NY"},"creationDate":1431620462,"isRegistered":false}')
GO

4、使用 JSON_VALUE 函数从 JSON 文本中提取值

SELECT JSON_VALUE(f.doc, '$.id')  AS Name, 
       JSON_VALUE(f.doc, '$.address.city') AS City,
       JSON_VALUE(f.doc, '$.address.county') AS County
FROM Families f 
WHERE JSON_VALUE(f.doc, '$.id') = N'AndersenFamily'
ORDER BY JSON_VALUE(f.doc, '$.address.city') DESC, JSON_VALUE(f.doc, '$.address.state') ASC

5、使用 JSON_QUERY 函数从 JSON 文本中提取对象或数组

SELECT JSON_QUERY(f.doc, '$.address') AS Address,
       JSON_QUERY(f.doc, '$.parents') AS Parents,
       JSON_QUERY(f.doc, '$.parents[0]') AS Parent0
FROM Families f 
WHERE JSON_VALUE(f.doc, '$.id') = N'AndersenFamily'

*
*
*

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值