Getting Unescaped JSON from SQL





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've created a stored procedure to pull data as a JSON object from my SQL Server database. All my data is relational and I'm trying to get it out as a JSON string.



Currently, I am able to get out a JSON string from SQL Server just fine, however this object ALWAYS includes escape characters (e.g. "{"field":"value"}). I'd like to pull the same JSON but without escaped characters. To test this I'm using some simple queries and getting them into .NET with a SqlDataAdapter using my stored procedure.



The thing that puzzles me is that when I run my query within SSMS, I never see any escape characters, but as soon as it's pulled a .NET application, the escape characters appear. I'd like to prevent this from happening and have my applications get only the unescaped JSON string.



I've tried several suggestions I've found during my research but nothing has produced my desired results. The changes I've seen (documented in MSDN and in other SO posts) have dealt with getting unescaped results, but only within SSMS and not within other applications.



What I've tried:



Simple Json query set to param and then using JSON_QUERY to select the param:



DECLARE @JSON varchar(max)

SET @JSON = (SELECT '{"Field":"Value"}' AS myJson FOR JSON PATH)

SELECT JSON_QUERY(@JSON) AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":[{"myJson":"{"Field":"Value"}"}]}]


Simple Json query without param using JSON_QUERY:



SELECT JSON_QUERY('{"Field":"Value"}') AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS



[{"JsonResponse":{"Field":"Value"}}]


Simple Json query with temp tables using JSON_QUERY:



CREATE TABLE #temp(
jsoncol varchar(255)
)

INSERT INTO #temp VALUES ('{"Field":"Value"}')

SELECT JSON_QUERY(jsoncol) AS 'JsonResponse' FROM #temp FOR JSON PATH

DROP TABLE #temp


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":{"Field":"Value"}}]


I'm lead to believe that there is no way to get out a JSON string from SQL Server without having the escaped characters. In case the examples above weren't enough, I've included my stored procedure here. Hopefully someone can point me in the right direction.



This is my stored procedure:



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_GetRoles]

@USER_ID int

AS
BEGIN

DECLARE @JSON varchar(max)

SET @JSON = (SELECT DISTINCT src.SOURCE_ID AS [source_id]
,RTRIM(src.SOURCE_CODE) AS [source_code]
,src.SOURCE_LABEL AS [source_label]
,(

SELECT srcapp.SOURCE_APPLICATION_ID AS [source_application_id]
,srcapp.APPLICATION_ID AS [application_id]
,app.APPLICATION_CODE AS [application_code]
,CASE WHEN srcappusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.SOURCE_APPLICATION AS srcapp
JOIN(SELECT APPLICATION_ID, APPLICATION_CODE FROM dbo.APPLICATION AS app WHERE RECORD_STATUS = 'A') app ON srcapp.APPLICATION_ID = app.APPLICATION_ID
JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
WHERE src.SOURCE_ID = srcapp.SOURCE_ID
AND srcappusr.USER_ID = @USER_ID
AND srcapp.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.applications]
,(

SELECT env.ENVIRONMENT_ID AS [environment_id]
,RTRIM(env.ENVIRONMENT_CODE) AS [environment_code]
,CASE WHEN envusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.ENVIRONMENT AS env
JOIN(SELECT ENVIRONMENT_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.ENVIRONMENT_USER AS envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID
WHERE src.SOURCE_ID = env.SOURCE_ID
AND envusr.USER_ID = @USER_ID
AND env.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.environments]
FROM dbo.SOURCE AS src

LEFT JOIN(SELECT SOURCE_ID, SOURCE_APPlICATION_ID FROM dbo.SOURCE_APPLICATION AS srcapp WHERE RECORD_STATUS = 'A') srcapp ON src.SOURCE_ID = srcapp.SOURCE_ID
LEFT JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
LEFT JOIN(SELECT SOURCE_ID, ENVIRONMENT_ID FROM dbo.ENVIRONMENT AS env WHERE RECORD_STATUS = 'A') env ON src.SOURCE_ID = env.SOURCE_ID
LEFT JOIN(SELECT ENVIRONMENT_ID, USER_ID FROM dbo.ENVIRONMENT_USER envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID

WHERE
(srcappusr.USER_ID = @USER_ID OR envusr.USER_ID = @USER_ID)
AND src.RECORD_STATUS = 'A'
FOR JSON PATH)

SELECT JSON_QUERY(@JSON) as 'JsonResponse'
END


This produces the following in a .NET application



"[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]"


But produces the following in SSMS



[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]









share|improve this question




















  • 2





    Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Nov 13 '18 at 17:15











  • @marc_s question about editing: why remove horizontal rules and header bolds? It makes the document very difficult to read now. Thanks for the mention on the sp_ prefix, I'll make sure to avoid doing that in the future.

    – expenguin
    Nov 13 '18 at 17:21













  • SQL server isn't adding the escape characters, .net is. if you're assigning the json response to a string in .net, .net is adding the escape characters. You'd have to deal with that on the .net side. Quick search stackoverflow.com/questions/16692371/…

    – Tim Mylott
    Nov 13 '18 at 21:15




















0















I've created a stored procedure to pull data as a JSON object from my SQL Server database. All my data is relational and I'm trying to get it out as a JSON string.



Currently, I am able to get out a JSON string from SQL Server just fine, however this object ALWAYS includes escape characters (e.g. "{"field":"value"}). I'd like to pull the same JSON but without escaped characters. To test this I'm using some simple queries and getting them into .NET with a SqlDataAdapter using my stored procedure.



The thing that puzzles me is that when I run my query within SSMS, I never see any escape characters, but as soon as it's pulled a .NET application, the escape characters appear. I'd like to prevent this from happening and have my applications get only the unescaped JSON string.



I've tried several suggestions I've found during my research but nothing has produced my desired results. The changes I've seen (documented in MSDN and in other SO posts) have dealt with getting unescaped results, but only within SSMS and not within other applications.



What I've tried:



Simple Json query set to param and then using JSON_QUERY to select the param:



DECLARE @JSON varchar(max)

SET @JSON = (SELECT '{"Field":"Value"}' AS myJson FOR JSON PATH)

SELECT JSON_QUERY(@JSON) AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":[{"myJson":"{"Field":"Value"}"}]}]


Simple Json query without param using JSON_QUERY:



SELECT JSON_QUERY('{"Field":"Value"}') AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS



[{"JsonResponse":{"Field":"Value"}}]


Simple Json query with temp tables using JSON_QUERY:



CREATE TABLE #temp(
jsoncol varchar(255)
)

INSERT INTO #temp VALUES ('{"Field":"Value"}')

SELECT JSON_QUERY(jsoncol) AS 'JsonResponse' FROM #temp FOR JSON PATH

DROP TABLE #temp


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":{"Field":"Value"}}]


I'm lead to believe that there is no way to get out a JSON string from SQL Server without having the escaped characters. In case the examples above weren't enough, I've included my stored procedure here. Hopefully someone can point me in the right direction.



This is my stored procedure:



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_GetRoles]

@USER_ID int

AS
BEGIN

DECLARE @JSON varchar(max)

SET @JSON = (SELECT DISTINCT src.SOURCE_ID AS [source_id]
,RTRIM(src.SOURCE_CODE) AS [source_code]
,src.SOURCE_LABEL AS [source_label]
,(

SELECT srcapp.SOURCE_APPLICATION_ID AS [source_application_id]
,srcapp.APPLICATION_ID AS [application_id]
,app.APPLICATION_CODE AS [application_code]
,CASE WHEN srcappusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.SOURCE_APPLICATION AS srcapp
JOIN(SELECT APPLICATION_ID, APPLICATION_CODE FROM dbo.APPLICATION AS app WHERE RECORD_STATUS = 'A') app ON srcapp.APPLICATION_ID = app.APPLICATION_ID
JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
WHERE src.SOURCE_ID = srcapp.SOURCE_ID
AND srcappusr.USER_ID = @USER_ID
AND srcapp.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.applications]
,(

SELECT env.ENVIRONMENT_ID AS [environment_id]
,RTRIM(env.ENVIRONMENT_CODE) AS [environment_code]
,CASE WHEN envusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.ENVIRONMENT AS env
JOIN(SELECT ENVIRONMENT_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.ENVIRONMENT_USER AS envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID
WHERE src.SOURCE_ID = env.SOURCE_ID
AND envusr.USER_ID = @USER_ID
AND env.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.environments]
FROM dbo.SOURCE AS src

LEFT JOIN(SELECT SOURCE_ID, SOURCE_APPlICATION_ID FROM dbo.SOURCE_APPLICATION AS srcapp WHERE RECORD_STATUS = 'A') srcapp ON src.SOURCE_ID = srcapp.SOURCE_ID
LEFT JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
LEFT JOIN(SELECT SOURCE_ID, ENVIRONMENT_ID FROM dbo.ENVIRONMENT AS env WHERE RECORD_STATUS = 'A') env ON src.SOURCE_ID = env.SOURCE_ID
LEFT JOIN(SELECT ENVIRONMENT_ID, USER_ID FROM dbo.ENVIRONMENT_USER envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID

WHERE
(srcappusr.USER_ID = @USER_ID OR envusr.USER_ID = @USER_ID)
AND src.RECORD_STATUS = 'A'
FOR JSON PATH)

SELECT JSON_QUERY(@JSON) as 'JsonResponse'
END


This produces the following in a .NET application



"[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]"


But produces the following in SSMS



[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]









share|improve this question




















  • 2





    Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Nov 13 '18 at 17:15











  • @marc_s question about editing: why remove horizontal rules and header bolds? It makes the document very difficult to read now. Thanks for the mention on the sp_ prefix, I'll make sure to avoid doing that in the future.

    – expenguin
    Nov 13 '18 at 17:21













  • SQL server isn't adding the escape characters, .net is. if you're assigning the json response to a string in .net, .net is adding the escape characters. You'd have to deal with that on the .net side. Quick search stackoverflow.com/questions/16692371/…

    – Tim Mylott
    Nov 13 '18 at 21:15
















0












0








0








I've created a stored procedure to pull data as a JSON object from my SQL Server database. All my data is relational and I'm trying to get it out as a JSON string.



Currently, I am able to get out a JSON string from SQL Server just fine, however this object ALWAYS includes escape characters (e.g. "{"field":"value"}). I'd like to pull the same JSON but without escaped characters. To test this I'm using some simple queries and getting them into .NET with a SqlDataAdapter using my stored procedure.



The thing that puzzles me is that when I run my query within SSMS, I never see any escape characters, but as soon as it's pulled a .NET application, the escape characters appear. I'd like to prevent this from happening and have my applications get only the unescaped JSON string.



I've tried several suggestions I've found during my research but nothing has produced my desired results. The changes I've seen (documented in MSDN and in other SO posts) have dealt with getting unescaped results, but only within SSMS and not within other applications.



What I've tried:



Simple Json query set to param and then using JSON_QUERY to select the param:



DECLARE @JSON varchar(max)

SET @JSON = (SELECT '{"Field":"Value"}' AS myJson FOR JSON PATH)

SELECT JSON_QUERY(@JSON) AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":[{"myJson":"{"Field":"Value"}"}]}]


Simple Json query without param using JSON_QUERY:



SELECT JSON_QUERY('{"Field":"Value"}') AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS



[{"JsonResponse":{"Field":"Value"}}]


Simple Json query with temp tables using JSON_QUERY:



CREATE TABLE #temp(
jsoncol varchar(255)
)

INSERT INTO #temp VALUES ('{"Field":"Value"}')

SELECT JSON_QUERY(jsoncol) AS 'JsonResponse' FROM #temp FOR JSON PATH

DROP TABLE #temp


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":{"Field":"Value"}}]


I'm lead to believe that there is no way to get out a JSON string from SQL Server without having the escaped characters. In case the examples above weren't enough, I've included my stored procedure here. Hopefully someone can point me in the right direction.



This is my stored procedure:



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_GetRoles]

@USER_ID int

AS
BEGIN

DECLARE @JSON varchar(max)

SET @JSON = (SELECT DISTINCT src.SOURCE_ID AS [source_id]
,RTRIM(src.SOURCE_CODE) AS [source_code]
,src.SOURCE_LABEL AS [source_label]
,(

SELECT srcapp.SOURCE_APPLICATION_ID AS [source_application_id]
,srcapp.APPLICATION_ID AS [application_id]
,app.APPLICATION_CODE AS [application_code]
,CASE WHEN srcappusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.SOURCE_APPLICATION AS srcapp
JOIN(SELECT APPLICATION_ID, APPLICATION_CODE FROM dbo.APPLICATION AS app WHERE RECORD_STATUS = 'A') app ON srcapp.APPLICATION_ID = app.APPLICATION_ID
JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
WHERE src.SOURCE_ID = srcapp.SOURCE_ID
AND srcappusr.USER_ID = @USER_ID
AND srcapp.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.applications]
,(

SELECT env.ENVIRONMENT_ID AS [environment_id]
,RTRIM(env.ENVIRONMENT_CODE) AS [environment_code]
,CASE WHEN envusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.ENVIRONMENT AS env
JOIN(SELECT ENVIRONMENT_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.ENVIRONMENT_USER AS envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID
WHERE src.SOURCE_ID = env.SOURCE_ID
AND envusr.USER_ID = @USER_ID
AND env.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.environments]
FROM dbo.SOURCE AS src

LEFT JOIN(SELECT SOURCE_ID, SOURCE_APPlICATION_ID FROM dbo.SOURCE_APPLICATION AS srcapp WHERE RECORD_STATUS = 'A') srcapp ON src.SOURCE_ID = srcapp.SOURCE_ID
LEFT JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
LEFT JOIN(SELECT SOURCE_ID, ENVIRONMENT_ID FROM dbo.ENVIRONMENT AS env WHERE RECORD_STATUS = 'A') env ON src.SOURCE_ID = env.SOURCE_ID
LEFT JOIN(SELECT ENVIRONMENT_ID, USER_ID FROM dbo.ENVIRONMENT_USER envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID

WHERE
(srcappusr.USER_ID = @USER_ID OR envusr.USER_ID = @USER_ID)
AND src.RECORD_STATUS = 'A'
FOR JSON PATH)

SELECT JSON_QUERY(@JSON) as 'JsonResponse'
END


This produces the following in a .NET application



"[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]"


But produces the following in SSMS



[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]









share|improve this question
















I've created a stored procedure to pull data as a JSON object from my SQL Server database. All my data is relational and I'm trying to get it out as a JSON string.



Currently, I am able to get out a JSON string from SQL Server just fine, however this object ALWAYS includes escape characters (e.g. "{"field":"value"}). I'd like to pull the same JSON but without escaped characters. To test this I'm using some simple queries and getting them into .NET with a SqlDataAdapter using my stored procedure.



The thing that puzzles me is that when I run my query within SSMS, I never see any escape characters, but as soon as it's pulled a .NET application, the escape characters appear. I'd like to prevent this from happening and have my applications get only the unescaped JSON string.



I've tried several suggestions I've found during my research but nothing has produced my desired results. The changes I've seen (documented in MSDN and in other SO posts) have dealt with getting unescaped results, but only within SSMS and not within other applications.



What I've tried:



Simple Json query set to param and then using JSON_QUERY to select the param:



DECLARE @JSON varchar(max)

SET @JSON = (SELECT '{"Field":"Value"}' AS myJson FOR JSON PATH)

SELECT JSON_QUERY(@JSON) AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":[{"myJson":"{"Field":"Value"}"}]}]


Simple Json query without param using JSON_QUERY:



SELECT JSON_QUERY('{"Field":"Value"}') AS 'JsonResponse' FOR JSON PATH


This produces the following in a .NET application



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS



[{"JsonResponse":{"Field":"Value"}}]


Simple Json query with temp tables using JSON_QUERY:



CREATE TABLE #temp(
jsoncol varchar(255)
)

INSERT INTO #temp VALUES ('{"Field":"Value"}')

SELECT JSON_QUERY(jsoncol) AS 'JsonResponse' FROM #temp FOR JSON PATH

DROP TABLE #temp


This produces the following in a .NET application:



"[{"JsonResponse":{"Field":"Value"}}]"


This produces the following in SSMS:



[{"JsonResponse":{"Field":"Value"}}]


I'm lead to believe that there is no way to get out a JSON string from SQL Server without having the escaped characters. In case the examples above weren't enough, I've included my stored procedure here. Hopefully someone can point me in the right direction.



This is my stored procedure:



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_GetRoles]

@USER_ID int

AS
BEGIN

DECLARE @JSON varchar(max)

SET @JSON = (SELECT DISTINCT src.SOURCE_ID AS [source_id]
,RTRIM(src.SOURCE_CODE) AS [source_code]
,src.SOURCE_LABEL AS [source_label]
,(

SELECT srcapp.SOURCE_APPLICATION_ID AS [source_application_id]
,srcapp.APPLICATION_ID AS [application_id]
,app.APPLICATION_CODE AS [application_code]
,CASE WHEN srcappusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.SOURCE_APPLICATION AS srcapp
JOIN(SELECT APPLICATION_ID, APPLICATION_CODE FROM dbo.APPLICATION AS app WHERE RECORD_STATUS = 'A') app ON srcapp.APPLICATION_ID = app.APPLICATION_ID
JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
WHERE src.SOURCE_ID = srcapp.SOURCE_ID
AND srcappusr.USER_ID = @USER_ID
AND srcapp.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.applications]
,(

SELECT env.ENVIRONMENT_ID AS [environment_id]
,RTRIM(env.ENVIRONMENT_CODE) AS [environment_code]
,CASE WHEN envusr.ADMINISTRATOR_FLAG = 'Y' THEN 'true' ELSE 'false' END AS [is_administrator]
FROM dbo.ENVIRONMENT AS env
JOIN(SELECT ENVIRONMENT_ID, USER_ID, ADMINISTRATOR_FLAG FROM dbo.ENVIRONMENT_USER AS envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID
WHERE src.SOURCE_ID = env.SOURCE_ID
AND envusr.USER_ID = @USER_ID
AND env.RECORD_STATUS = 'A'
FOR JSON PATH

) AS [source.environments]
FROM dbo.SOURCE AS src

LEFT JOIN(SELECT SOURCE_ID, SOURCE_APPlICATION_ID FROM dbo.SOURCE_APPLICATION AS srcapp WHERE RECORD_STATUS = 'A') srcapp ON src.SOURCE_ID = srcapp.SOURCE_ID
LEFT JOIN(SELECT SOURCE_APPLICATION_ID, USER_ID FROM dbo.SOURCE_APPLICATION_USER AS srcappusr WHERE RECORD_STATUS = 'A') srcappusr ON srcapp.SOURCE_APPLICATION_ID = srcappusr.SOURCE_APPLICATION_ID
LEFT JOIN(SELECT SOURCE_ID, ENVIRONMENT_ID FROM dbo.ENVIRONMENT AS env WHERE RECORD_STATUS = 'A') env ON src.SOURCE_ID = env.SOURCE_ID
LEFT JOIN(SELECT ENVIRONMENT_ID, USER_ID FROM dbo.ENVIRONMENT_USER envusr WHERE RECORD_STATUS = 'A') envusr ON env.ENVIRONMENT_ID = envusr.ENVIRONMENT_ID

WHERE
(srcappusr.USER_ID = @USER_ID OR envusr.USER_ID = @USER_ID)
AND src.RECORD_STATUS = 'A'
FOR JSON PATH)

SELECT JSON_QUERY(@JSON) as 'JsonResponse'
END


This produces the following in a .NET application



"[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]"


But produces the following in SSMS



[{"source_id":1,"source_code":"1","source_label":"SOURCE1","source":{"applications":[{"source_application_id":1,"application_id":1,"application_code":"APP1","is_administrator":"true"}],"environments":[{"environment_id":1,"environment_code":"1","is_administrator":"true"}]}}]






sql json sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:15









marc_s

584k13011241270




584k13011241270










asked Nov 13 '18 at 17:07









expenguinexpenguin

168114




168114








  • 2





    Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Nov 13 '18 at 17:15











  • @marc_s question about editing: why remove horizontal rules and header bolds? It makes the document very difficult to read now. Thanks for the mention on the sp_ prefix, I'll make sure to avoid doing that in the future.

    – expenguin
    Nov 13 '18 at 17:21













  • SQL server isn't adding the escape characters, .net is. if you're assigning the json response to a string in .net, .net is adding the escape characters. You'd have to deal with that on the .net side. Quick search stackoverflow.com/questions/16692371/…

    – Tim Mylott
    Nov 13 '18 at 21:15
















  • 2





    Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Nov 13 '18 at 17:15











  • @marc_s question about editing: why remove horizontal rules and header bolds? It makes the document very difficult to read now. Thanks for the mention on the sp_ prefix, I'll make sure to avoid doing that in the future.

    – expenguin
    Nov 13 '18 at 17:21













  • SQL server isn't adding the escape characters, .net is. if you're assigning the json response to a string in .net, .net is adding the escape characters. You'd have to deal with that on the .net side. Quick search stackoverflow.com/questions/16692371/…

    – Tim Mylott
    Nov 13 '18 at 21:15










2




2





Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

– marc_s
Nov 13 '18 at 17:15





Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

– marc_s
Nov 13 '18 at 17:15













@marc_s question about editing: why remove horizontal rules and header bolds? It makes the document very difficult to read now. Thanks for the mention on the sp_ prefix, I'll make sure to avoid doing that in the future.

– expenguin
Nov 13 '18 at 17:21







@marc_s question about editing: why remove horizontal rules and header bolds? It makes the document very difficult to read now. Thanks for the mention on the sp_ prefix, I'll make sure to avoid doing that in the future.

– expenguin
Nov 13 '18 at 17:21















SQL server isn't adding the escape characters, .net is. if you're assigning the json response to a string in .net, .net is adding the escape characters. You'd have to deal with that on the .net side. Quick search stackoverflow.com/questions/16692371/…

– Tim Mylott
Nov 13 '18 at 21:15







SQL server isn't adding the escape characters, .net is. if you're assigning the json response to a string in .net, .net is adding the escape characters. You'd have to deal with that on the .net side. Quick search stackoverflow.com/questions/16692371/…

– Tim Mylott
Nov 13 '18 at 21:15














1 Answer
1






active

oldest

votes


















1














This depends where you look at the string...



In SSMS a string is marked with single quotes. The double quote can exist within a string without problems:



DECLARE @SomeString = 'This can include "double quotes" but you have to double ''single quote''';


In a C# application the double quote is the string marker. So the above example would look like this:



string SomeString = "This must escape "double quotes" but you can use 'single quote' without problems";


Within your IDE (is it VS?) you can look at the string as is or as you'd need to be used in code. Your example shows " at the beginning and at the end of your string. That is a clear hint, that this is the option as in code. You could use this string and place it into your code. The real string, which is used and processed will not contain escape characters.



Hint: Escape characters are only needed in human-readable formats, where there are characters with special meaning (a ; in a CSV, a < in HTML and so on)...



UPDATE Some more explanation



Escape characters are needed to place a string within a string. Somehow you have to mark the beginning and the end of the string, but there is nothing else you can use then some magic characters.



In order to use these characters within the embedded string you have to go one the following ways:




  1. escaping (e.g. XML will replace & with &amp; and JSON will replace a " with " as JSON uses the " to mark its labels) or


  2. Magic borders (e.g. a CDATA-section in XML, which allows to place unescaped characters as is: <![CDATA[forbidden characters &<> allowed here]]>)


Whatever you do, you must distinguish between the visible string in an editor or in a text-based container like XML or JSON and the value the application will pick out of this.



An example:



<root><a>this &amp; that</a></root> 
visible string: "this &amp; that"
real value: "this & that"





share|improve this answer


























  • Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

    – expenguin
    Nov 14 '18 at 20:48













  • @expenguin I do not know, if you really got the ghist... See my update

    – Shnugo
    Nov 16 '18 at 10:31












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286203%2fgetting-unescaped-json-from-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This depends where you look at the string...



In SSMS a string is marked with single quotes. The double quote can exist within a string without problems:



DECLARE @SomeString = 'This can include "double quotes" but you have to double ''single quote''';


In a C# application the double quote is the string marker. So the above example would look like this:



string SomeString = "This must escape "double quotes" but you can use 'single quote' without problems";


Within your IDE (is it VS?) you can look at the string as is or as you'd need to be used in code. Your example shows " at the beginning and at the end of your string. That is a clear hint, that this is the option as in code. You could use this string and place it into your code. The real string, which is used and processed will not contain escape characters.



Hint: Escape characters are only needed in human-readable formats, where there are characters with special meaning (a ; in a CSV, a < in HTML and so on)...



UPDATE Some more explanation



Escape characters are needed to place a string within a string. Somehow you have to mark the beginning and the end of the string, but there is nothing else you can use then some magic characters.



In order to use these characters within the embedded string you have to go one the following ways:




  1. escaping (e.g. XML will replace & with &amp; and JSON will replace a " with " as JSON uses the " to mark its labels) or


  2. Magic borders (e.g. a CDATA-section in XML, which allows to place unescaped characters as is: <![CDATA[forbidden characters &<> allowed here]]>)


Whatever you do, you must distinguish between the visible string in an editor or in a text-based container like XML or JSON and the value the application will pick out of this.



An example:



<root><a>this &amp; that</a></root> 
visible string: "this &amp; that"
real value: "this & that"





share|improve this answer


























  • Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

    – expenguin
    Nov 14 '18 at 20:48













  • @expenguin I do not know, if you really got the ghist... See my update

    – Shnugo
    Nov 16 '18 at 10:31
















1














This depends where you look at the string...



In SSMS a string is marked with single quotes. The double quote can exist within a string without problems:



DECLARE @SomeString = 'This can include "double quotes" but you have to double ''single quote''';


In a C# application the double quote is the string marker. So the above example would look like this:



string SomeString = "This must escape "double quotes" but you can use 'single quote' without problems";


Within your IDE (is it VS?) you can look at the string as is or as you'd need to be used in code. Your example shows " at the beginning and at the end of your string. That is a clear hint, that this is the option as in code. You could use this string and place it into your code. The real string, which is used and processed will not contain escape characters.



Hint: Escape characters are only needed in human-readable formats, where there are characters with special meaning (a ; in a CSV, a < in HTML and so on)...



UPDATE Some more explanation



Escape characters are needed to place a string within a string. Somehow you have to mark the beginning and the end of the string, but there is nothing else you can use then some magic characters.



In order to use these characters within the embedded string you have to go one the following ways:




  1. escaping (e.g. XML will replace & with &amp; and JSON will replace a " with " as JSON uses the " to mark its labels) or


  2. Magic borders (e.g. a CDATA-section in XML, which allows to place unescaped characters as is: <![CDATA[forbidden characters &<> allowed here]]>)


Whatever you do, you must distinguish between the visible string in an editor or in a text-based container like XML or JSON and the value the application will pick out of this.



An example:



<root><a>this &amp; that</a></root> 
visible string: "this &amp; that"
real value: "this & that"





share|improve this answer


























  • Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

    – expenguin
    Nov 14 '18 at 20:48













  • @expenguin I do not know, if you really got the ghist... See my update

    – Shnugo
    Nov 16 '18 at 10:31














1












1








1







This depends where you look at the string...



In SSMS a string is marked with single quotes. The double quote can exist within a string without problems:



DECLARE @SomeString = 'This can include "double quotes" but you have to double ''single quote''';


In a C# application the double quote is the string marker. So the above example would look like this:



string SomeString = "This must escape "double quotes" but you can use 'single quote' without problems";


Within your IDE (is it VS?) you can look at the string as is or as you'd need to be used in code. Your example shows " at the beginning and at the end of your string. That is a clear hint, that this is the option as in code. You could use this string and place it into your code. The real string, which is used and processed will not contain escape characters.



Hint: Escape characters are only needed in human-readable formats, where there are characters with special meaning (a ; in a CSV, a < in HTML and so on)...



UPDATE Some more explanation



Escape characters are needed to place a string within a string. Somehow you have to mark the beginning and the end of the string, but there is nothing else you can use then some magic characters.



In order to use these characters within the embedded string you have to go one the following ways:




  1. escaping (e.g. XML will replace & with &amp; and JSON will replace a " with " as JSON uses the " to mark its labels) or


  2. Magic borders (e.g. a CDATA-section in XML, which allows to place unescaped characters as is: <![CDATA[forbidden characters &<> allowed here]]>)


Whatever you do, you must distinguish between the visible string in an editor or in a text-based container like XML or JSON and the value the application will pick out of this.



An example:



<root><a>this &amp; that</a></root> 
visible string: "this &amp; that"
real value: "this & that"





share|improve this answer















This depends where you look at the string...



In SSMS a string is marked with single quotes. The double quote can exist within a string without problems:



DECLARE @SomeString = 'This can include "double quotes" but you have to double ''single quote''';


In a C# application the double quote is the string marker. So the above example would look like this:



string SomeString = "This must escape "double quotes" but you can use 'single quote' without problems";


Within your IDE (is it VS?) you can look at the string as is or as you'd need to be used in code. Your example shows " at the beginning and at the end of your string. That is a clear hint, that this is the option as in code. You could use this string and place it into your code. The real string, which is used and processed will not contain escape characters.



Hint: Escape characters are only needed in human-readable formats, where there are characters with special meaning (a ; in a CSV, a < in HTML and so on)...



UPDATE Some more explanation



Escape characters are needed to place a string within a string. Somehow you have to mark the beginning and the end of the string, but there is nothing else you can use then some magic characters.



In order to use these characters within the embedded string you have to go one the following ways:




  1. escaping (e.g. XML will replace & with &amp; and JSON will replace a " with " as JSON uses the " to mark its labels) or


  2. Magic borders (e.g. a CDATA-section in XML, which allows to place unescaped characters as is: <![CDATA[forbidden characters &<> allowed here]]>)


Whatever you do, you must distinguish between the visible string in an editor or in a text-based container like XML or JSON and the value the application will pick out of this.



An example:



<root><a>this &amp; that</a></root> 
visible string: "this &amp; that"
real value: "this & that"






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 10:31

























answered Nov 14 '18 at 16:51









ShnugoShnugo

50.9k72772




50.9k72772













  • Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

    – expenguin
    Nov 14 '18 at 20:48













  • @expenguin I do not know, if you really got the ghist... See my update

    – Shnugo
    Nov 16 '18 at 10:31



















  • Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

    – expenguin
    Nov 14 '18 at 20:48













  • @expenguin I do not know, if you really got the ghist... See my update

    – Shnugo
    Nov 16 '18 at 10:31

















Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

– expenguin
Nov 14 '18 at 20:48







Hey, thanks for the reply. To add to this, we actually use the JSON response and create a token in ADFS. Using JWT (jwt.io) to check the token, the escape characters are included within when it's decoded. I was using .NET in order to view the incoming object and noticed the same behavior. I was hoping to have the JSON in the token be escapeless, but from what you're telling me, I don't know if that's possible since the escape characters are dependent on the receiving application. How can I view the original string without escapes? Is it just a matter of typing my variable?

– expenguin
Nov 14 '18 at 20:48















@expenguin I do not know, if you really got the ghist... See my update

– Shnugo
Nov 16 '18 at 10:31





@expenguin I do not know, if you really got the ghist... See my update

– Shnugo
Nov 16 '18 at 10:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286203%2fgetting-unescaped-json-from-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values