Skip to main content
April 20, 2021
Solved

Converting dictionary to a different JSON response

  • April 20, 2021
  • 4 replies
  • 0 views

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!
Best answer by stefanhelzle0001

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
April 20, 2021

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

April 20, 2021

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

April 20, 2021

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\"