SQL, if you encounter Json, how would you do?

1, Json string as follows:

DECLARE @JsonInfo NVARCHAR(2000)
SET @JsonInfo=N'
                {
                    "CalcPayInput":{
                        "ServiceCode":"2131296",
                        "Items":[
                            {
                                "Length":10,
                                "Width":50,
                                "High":80
                            } 
                        ] 
                    }, 
                    " BusinessBody " : {
                         " WaybillNumber " : " SZY222019082001135 " 
                    } 
                } 
                '   
the SELECT Len (@JsonInfo) 


/ * actual storage is recommended to use this, save compression line * / 
the SET @JsonInfo = N ' { "CalcPayInput": { "ServiceCode": "2131296" , "Items": [{ "Length": 10, "Width": 50, "High": 80}]}, "BusinessBody": { "WaybillNumber": "SZY222019082001135"}} '  
Only SELECT (@JsonInfo)

 

2, the value obtained json string field ServiceCode

Note: SQL SERVER 2016 later versions support (older versions need to upgrade) before parsing verify whether it is Json Json statement select ISJSON (@JsonInfo) If the string contains a valid JSON, it returns 1 ; otherwise, it returns 0 . If the expression is NULL, NULL is returned. 

JSON_VALUE the SELECT (@JsonInfo, ' $ .CalcPayInput.ServiceCode ' ) ServiceCode

 

3, obtaining the value of the Length field string json

Note: hierarchy, the Items are set, the first set of acquired, [ 0 ], the index starts from 0 

the SELECT JSON_VALUE (@JsonInfo, ' $ .CalcPayInput.Items [0] .Length ' ) the Length

 

4, to obtain the value field json string WaybillNumber

Note: Json string of a first plurality of sub-object layer is present 

the SELECT JSON_VALUE (@JsonInfo, ' $ .BusinessBody.WaybillNumber ' ) WaybillNumber

 

5, modified WaybillNumber json string field value YZY222019082001135

Note: Json string of a first plurality of sub-object layer is present 

 the SELECT JSON_MODIFY (@JsonInfo, ' $ .BusinessBody.WaybillNumber ' , ' YZY222019082001135 ' ) JsonInfo

 

6, the string is removed WaybillNumber field json

Note: positioning unneeded fields to use JSON_MODIFY function is set to NULL 

the SELECT JSON_MODIFY (@JsonInfo, ' $ .BusinessBody.WaybillNumber ' , null ) JsonInfo

 

7, adding json string field ProductCode

Note: positioning unneeded fields to use JSON_MODIFY function adds 

the SELECT JSON_MODIFY (@JsonInfo, ' $ .BusinessBody.ProductCode ' , ' 256 354 ' ) JsonInfo

 

8, reference documentation

https://docs.microsoft.com/zh-cn/sql/t-sql/functions/isjson-transact-sql?view=sql-server-2017
https://docs.microsoft.com/zh-cn/sql/t-sql/functions/json-value-transact-sql?view=sql-server-2017
https://docs.microsoft.com/zh-cn/sql/t-sql/functions/json-query-transact-sql?view=sql-server-2017
https://docs.microsoft.com/zh-cn/sql/t-sql/functions/json-modify-transact-sql?view=sql-server-2017

 

Micro letter: 13722885914 View my albums 
Jane book: HTTPS: // www.jianshu.com/u/dbfbe12c187d 
welcome to add me, ask questions, solve the best of my ability, I know.

 

Guess you like

Origin www.cnblogs.com/zhanfuzhi/p/zhan3.html