Skip to main content
New Participant
Apr 20, 2021 11:50 AM UTC
Solved

Converting dictionary to a different JSON response

Hi,

How can I convert the below dictionary-

{
name:"Vivek",
place:"XYZ"
}
to something like this:
"{\"name\":\"Vivek\",\"place\":\"XYZ\"}"
It is a bit from what a!toJson function returns. Any lead will be helpful.
Thanks!
stefanhelzle0001
Brainy
Apr 20, 2021 12:11 PM UTC

What is your use case?

If you really need to do that, you can do a toJson() followed by a substitute() call to replace " with \".

Feels like you want to escape a JSON string. For this you would need to escape more characters. 

docs.microsoft.com/.../how-for-json-escapes-special-characters-and-control-characters-sql-server

4 replies

stefanhelzle0001
Brainy
Apr 20, 2021 12:11 PM UTC

What is your use case?

If you really need to do that, you can do a toJson() followed by a substitute() call to replace " with \".

Feels like you want to escape a JSON string. For this you would need to escape more characters. 

docs.microsoft.com/.../how-for-json-escapes-special-characters-and-control-characters-sql-server

New Participant
Apr 20, 2021 12:17 PM UTC

Yes I want to escape the quotes in json string. Will the substitute function will help achieve this?

Inspiring
Apr 20, 2021 12:34 PM UTC

Hi Vivek

Please try the below code  which is closer to your output.

a!localVariables(
local!data:{
{name:"Vivek",
place:"XYZ"}
} ,
local!final:substitute(insertkeyval("name",local!data["name"],"\","\"),"=","\:\"),
local!final2:substitute(insertkeyval("place",local!data["place"],"\","\"),"=","\:\"),
local!data2:concat(local!final,",",local!final2),
local!data2

)

sample output "\name\:\Vivek\,\place\:\XYZ\"