server sql 去 反斜杠,从JSON SQL查询中删除反斜杠

本文介绍了如何使用SQL Server查询构造JSON对象时遇到的反斜杠问题,并提供了解决方案。当FOR JSON PATH在生成JSON输出时自动转义特殊字符时,可以使用JSON_QUERY来避免这种情况。示例展示了如何通过JSON_QUERY消除反斜杠,从而得到预期的无反斜杠格式的JSON输出。
摘要由CSDN通过智能技术生成

I am constructing JSON object using SQL Server query. I have issues with slashes and also i need data in specific format as mentioned below. The actual output has slashes in it. I want to eliminate slashes from my output query.

SELECT (SELECT

('{'+'"value":'+ cast(ISNULL(SP.ACCOUNT_TRAFFIC_DEP_LICENSE_NUMBER,'') as varchar) ) AS TRAFFIC_DEP_LICENSE_NUMBER

,( '{' +'"value":'+cast(ISNULL(SP.NUMBER_OF_VEHICLES ,'') as varchar) ) AS NUMBER_OF_VEHICLES

,( '{' +'"value":'+cast(ISNULL(SP.ACCOUNT_TRAFFIC_DEP_LICENSE_START_H_DATE,'') as varchar) ) AS TRAFFIC_DEP_LICENSE_START_H_DATE

,( '{' +'"value":'+cast(ISNULL(SP.ACCOUNT_TRAFFIC_DEP_LICENSE_END_H_DATE ,'') as varchar) ) AS TRAFFIC_DEP_LICENSE_END_H_DATE

,( '{' +'"value":'+cast(ISNULL(SP.SUSPENSION_BY_MORROR_STATUS,'') as varchar) ) AS SUSPENSION_BY_MORROR_STATUS

,( '{' +'"value":'+cast(ISNULL(SP.SUSPENSION_BY_VIOLATION ,'') as varchar) ) AS SUSPENSION_BY_VIOLATION

,( '{' +'"value":'+cast(ISNULL(SP.SUSPENSION_BY_PENALTY ,'') as varchar) ) AS SUSPENSION_BY_PENALTY

from [dbo].[TAMM_CAC_ACCOUNTS_STG] SP

where SP.Account_Reference_Number = CAC.Account_Reference_Number

AND (

ACCOUNT_TRAFFIC_DEP_LICENSE_NUMBER IS NOT NULL OR

NUMBER_OF_VEHICLES IS NOT NULL OR

SP.ACCOUNT_TRAFFIC_DEP_LICENSE_END_H_DATE IS NOT NULL OR

SP.ACCOUNT_TRAFFIC_DEP_LICENSE_END_H_DATE IS NOT NULL OR

SP.SUSPENSION_BY_MORROR_STATUS IS NOT NULL OR

SP.SUSPENSION_BY_VIOLATION IS NOT NULL OR

SP.SUSPENSION_BY_PENALTY IS NOT NULL ) FOR JSON PATH,INCLUDE_NULL_VALUES

) AS specifics

from dbo.Customer_Account CAC

The above query gives me desired output but It has backslashes.

[

{

"TRAFFIC_DEP_LICENSE_NUMBER": "{\"value\":0",

"NUMBER_OF_VEHICLES": "{\"value\":1009",

"TRAFFIC_DEP_LICENSE_START_H_DATE": "{\"value\":14351229",

"TRAFFIC_DEP_LICENSE_END_H_DATE": "{\"value\":14351229",

"SUSPENSION_BY_MORROR_STATUS": "{\"value\":0",

"SUSPENSION_BY_VIOLATION": "{\"value\":1",

"SUSPENSION_BY_PENALTY": "{\"value\":1"

}

]

actual expected output is below

“specific”:{

"TRAFFIC_DEP_LICENSE_NUMBER":{“value”:0},

"NUMBER_OF_VEHICLES":{“value”:22},

"TRAFFIC_DEP_LICENSE_START_H_DATE":{“value”: 14480608},

"TRAFFIC_DEP_LICENSE_END_H_DATE":{“value”: 14480608},

"SUSPENSION_BY_MORROR_STATUS":{“value”:0},

"SUSPENSION_BY_VIOLATION":{“value”:1},

"SUSPENSION_BY_PENALTY":{“value”:1},

}

解决方案

If I understand your question correctly and you want to remove escaping of the special characters to generate a valid JSON content, next solution may help.

FOR JSON clause escapes special characters in the JSON output with \.

... If the source data contains special characters, the FOR JSON

clause escapes them in the JSON output with \, as shown in the

following table. This escaping occurs both in the names of properties

and in their values. ...

If you are sure, that you generate a valid JSON, you may try to use JSON_QUERY with FOR JSON.

... JSON_QUERY returns a valid JSON fragment. As a result, FOR JSON

doesn't escape special characters in the JSON_QUERY return value. ...

Simple example, that demonstrates your issue:

-- Statement:

SELECT (

SELECT '{"value": 1}' AS TRAFFIC_DEP_LICENSE_NUMBER

FOR JSON PATH, INCLUDE_NULL_VALUES

) AS specifics

-- Output:

-------------------------------------------------

specifics

-------------------------------------------------

[{"TRAFFIC_DEP_LICENSE_NUMBER":"{\"value\": 1}"}]

Solution:

-- Statement:

SELECT (

SELECT JSON_QUERY('{"value": 1}') AS TRAFFIC_DEP_LICENSE_NUMBER

FOR JSON PATH, INCLUDE_NULL_VALUES

) AS specifics

-- Output:

-------------------------------------------------

specifics

-------------------------------------------------

[{"TRAFFIC_DEP_LICENSE_NUMBER":{"value": 1}}]

Notes:

You may check your JSON using ISJSON:

SELECT (

SELECT CASE

WHEN ISJSON('{"value": 1}') = 1 THEN JSON_QUERY('{"value": 1}')

ELSE 'Invalid JSON'

END AS TRAFFIC_DEP_LICENSE_NUMBER

FOR JSON PATH, INCLUDE_NULL_VALUES

) AS specifics

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值