提取内部 JSON 子对象

OPENJSON 可以在 JSON 文本中提取 JSON 对象的片段。在引用 JSON 子对象的列定义中,设置类型 nvarchar(max) 和 AS JSON 选项:

declare @json nvarchar(4000) = N'[
  {"Number":"SO43659","Date":"2011-05-31T00:00:00","info":{"customer":"MSFT","Price":59.99,"qty":1}},
  {"Number":"SO43661","Date":"2011-06-01T00:00:00","info":{"customer":"Nokia","Price":24.99,"qty":3}}
]'

SELECT    * 
FROM OPENJSON (@json)
    WITH (
          Number   varchar(200),
          Date     datetime,
          Info nvarchar(max) '$.info' AS JSON
  )

信息列将映射到信息对象。结果将是:

日期 信息
SO43659 2011-05-31T00:00:00 { 客户MSFT价格:59.99, 数量:1}
SO43661 2011-06-01T00:00:00 { 客户诺基亚价格:24.99, 数量:3}