다음을 통해 공유


JSON_VALUE (Transact-SQL)

Applies to: SQL Server 2016 (13.x) and later versions Azure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSQL analytics endpoint in Microsoft FabricWarehouse in Microsoft FabricSQL database in Microsoft Fabric

구문은 JSON_VALUE JSON 문자열에서 스칼라 값을 추출합니다.

To extract an object or an array from a JSON string instead of a scalar value, see JSON_QUERY (Transact-SQL). JSON_VALUE와(과) JSON_QUERY의 차이점에 대한 정보는, JSON_VALUE 및 JSON_QUERY 비교를 참조하세요.

Transact-SQL 구문 표기 규칙

Syntax

JSON_VALUE ( expression , path )  

Arguments

expression

An expression. 일반적으로 JSON 텍스트를 포함하는 변수 또는 열의 이름입니다.

If JSON_VALUE finds JSON that is not valid in expression before it finds the value identified by path, the function returns an error. If JSON_VALUE doesn't find the value identified by path, it scans the entire text and returns an error if it finds JSON that is not valid anywhere in expression.

path

추출할 속성을 지정하는 JSON 경로입니다. 자세한 내용은 JSON 경로 식을 참조하세요.

In SQL Server 2017 (14.x) and in Azure SQL Database, you can provide a variable as the value of path.

If the format of path isn't valid, JSON_VALUE returns an error.

Return value

nvarchar(4000) 형식의 단일 텍스트 값을 반환합니다. 반환된 값의 데이터 정렬은 입력된 식의 데이터 정렬과 동일합니다.

값이 4000자를 초과하는 경우:

  • lax 모드에서는 JSON_VALUE이(가) NULL를 반환합니다.

  • strict 모드에서는 JSON_VALUE이(가) 오류를 반환합니다.

4000자보다 큰 스칼라 값을 반환해야 하는 경우 대신 사용합니다 OPENJSONJSON_VALUE. For more info, see OPENJSON (Transact-SQL).

JSON functions work the same whether the JSON document is stored in varchar, nvarchar, or the native json data type.

Remarks

lax 모드와 strict 모드

다음 JSON 텍스트를 살펴보십시오.

DECLARE @jsonInfo NVARCHAR(MAX)

SET @jsonInfo=N'{  
     "info":{    
       "type":1,  
       "address":{    
         "town":"Bristol",  
         "county":"Avon",  
         "country":"England"  
       },  
       "tags":["Sport", "Water polo"]  
    },  
    "type":"Basic"  
 }'  

다음 표에서는 lax 모드 및 strict 모드에서 JSON_VALUE의 동작을 비교합니다. 선택적 경로 모드 사양(lax 또는 strict)에 대한 자세한 내용은 JSON 경로 식을 참조하세요.

Path lax 모드에서 값을 반환합니다. strict 모드에서 값을 반환합니다. More info
$ NULL Error 스칼라 값이 아닙니다.

대신 JSON_QUERY를 사용하세요.
$.info.type N'1' N'1' N/a
$.info.address.town N'Bristol' N'Bristol' N/a
$.info."address" NULL Error 스칼라 값이 아닙니다.

대신 JSON_QUERY를 사용하세요.
$.info.tags NULL Error 스칼라 값이 아닙니다.

대신 JSON_QUERY를 사용하세요.
$.info.type[0] NULL Error 배열이 아닙니다.
$.info.none NULL Error 속성이 없습니다.

Examples

Example 1

다음 예에서는 쿼리 결과에서 JSON 속성 townstate의 값을 사용합니다. JSON_VALUE은(는) 원본의 데이터 정렬을 유지하므로 결과의 정렬 순서는 jsonInfo 열의 데이터 정렬에 따라 다릅니다.

Note

(이 예제에서는 Person.Person이라는 테이블에 JSON 텍스트의 jsonInfo 열이 포함되어 있고, 이 열에는 이전에 lax 모드 및 strict 모드에 대해 설명한 것과 같은 구조가 있다고 가정합니다. AdventureWorks 샘플 데이터베이스에서 Person 테이블에는 실제로 jsonInfo 열이 포함되어 있지 않습니다.)

SELECT FirstName, LastName,
 JSON_VALUE(jsonInfo,'$.info.address.town') AS Town
FROM Person.Person
WHERE JSON_VALUE(jsonInfo,'$.info.address.state') LIKE 'US%'
ORDER BY JSON_VALUE(jsonInfo,'$.info.address.town')

Example 2

다음 예에서는 JSON 속성 town의 값을 로컬 변수로 추출합니다.

DECLARE @jsonInfo NVARCHAR(MAX)
DECLARE @town NVARCHAR(32)

SET @jsonInfo=N'{"info":{"address":[{"town":"Paris"},{"town":"London"}]}}';

SET @town=JSON_VALUE(@jsonInfo,'$.info.address[0].town'); -- Paris
SET @town=JSON_VALUE(@jsonInfo,'$.info.address[1].town'); -- London

Example 3

다음 예에서는 JSON 속성 값을 기반으로 계산된 열을 만듭니다.

CREATE TABLE dbo.Store
 (
  StoreID INT IDENTITY(1,1) NOT NULL,
  Address VARCHAR(500),
  jsonContent NVARCHAR(4000),
  Longitude AS JSON_VALUE(jsonContent, '$.address[0].longitude'),
  Latitude AS JSON_VALUE(jsonContent, '$.address[0].latitude')
 )