SQL Server

SQL Server JSON 함수(Transact-SQL)

DBASeoul 2023. 1. 11. 10:48

기능Description

ISJSON 문자열에 유효한 JSON이 포함되어 있는지 테스트합니다.
JSON_VALUE JSON 문자열에서 스칼라 값을 추출합니다.
JSON_QUERY JSON 문자열에서 개체 또는 배열을 추출합니다.
JSON_MODIFY JSON 문자열의 속성 값을 업데이트하고 업데이트된 JSON 문자열을 반환합니다.
JSON_PATH_EXISTS 지정된 SQL/JSON 경로가 입력 JSON 문자열에 있는지 여부를 테스트합니다.

 

create table JSON_table (
seq int identity(1,1) ,
reg_date datetime ,
JSON nvarchar(1000)
)
insert into JSON_table (reg_date,JSON) values (getdate(),
'
{
       "UserLevel":1,"Item":[
              {"id":20030,"type":2,"count":2},
              {"id":20024,"type":3,"count":4}],
       "Money":
       {
              "Type":1,"value":1000},"Change":2
       }
       
       ')
SELECT * FROM JSON_table
SELECT *
FROM RV_DATE  
FOR JSON AUTO;
select seq ,reg_date ,
JSON_VALUE(JSON,'$.UserLevel') as UserLevel ,
JSON_VALUE(JSON,'$.Item[1].id') as Item_id1,
JSON_VALUE(JSON,'$.Item[0].id') as Item_id0
from JSON_table

 

JSON 문자열에서 개체 또는 배열을 추출합니다

select top 10 JSON_QUERY(Prod_Json, '$.domeggook.basis.keywords.kw'),* from StorePrduct

결과

 

 

JSON_QUERY 서브 노드 JSON 가져오기

declare @json nvarchar(max) = N'
{
	"Node Type": "Aggregate",
	"Strategy": "Plain",
	"Partial Mode": "Simple",
	"Parent Relationship": "Inner",
	"Parallel Aware": false,
	"Async Capable": false,
	"Startup Cost": 12.46,
	"Total Cost": 12.47,
	"Plan Rows": 1,
	"Plan Width": 8,
	"Actual Startup Time": 0.008,
	"Actual Total Time": 0.008,
	"Actual Rows": 1,
	"Actual Loops": 100,
	"Plans": [
		{
			"Node Type": "Index Scan",
			"Parent Relationship": "Outer",
			"Parallel Aware": false,
			"Async Capable": false,
			"Scan Direction": "Forward",
			"Index Name": "ix_system_id",
			"Relation Name": "test1m",
			"Alias": "b",
			"Startup Cost": 0.42,
			"Total Cost": 12.46,
			"Plan Rows": 2,
			"Plan Width": 4,
			"Actual Startup Time": 0.007,
			"Actual Total Time": 0.007,
			"Actual Rows": 2,
			"Actual Loops": 100,
			"Index Cond": "((system_id)::text = (a.system_id)::text)",
			"Rows Removed by Index Recheck": 0
		}
	]
}'


select JSON_QUERY(@json,'$.Plans[0]')