I have a text with embedded double quotes and a JSON file has to be created. The file has the double quotes between text but while loading this file, the process fails as the parsing is not working. I would like to replace starting double quotes with \" and ending double quotes with \".
e.g. Original text -> Is a "new" version
Replaced text (desired) -> Is a \"new\" version
Once the text is modified the JSON file can parse the embedded quotes.
Hey @vibhas! There are many ways to achieve this, both inside and outside of SAS. Can you describe where your process is failing? Can you post your code and error message?
The problem is that a JSON file has a LOT of quotes already, so you need to be able to find the ones that are inside and so need to be protected.
Can you share two small example JSON files that represents what have now and what you want to create?
Also it is not clear to me if you are writing the JSON file using SAS or reading the JSON file using SAS? Or perhaps you have an existing JSON file you just want to use SAS to process so it can be read by something else?
In SAS an embedded quote should be represented by doubled quotes. So to represent the string:
Is a "new" version
You would use this code:
"Is a ""new"" version"
But in the Unix command shell style that JSON uses you would instead "escape" the special characters with a backslash so that you would use this code:
"Is a \"new\" version"
If you have the "" already then you just want to change the "" to \" instead.
But might need special logic to handle the cases where the inside quote is at the start or end of the string. You want something like this:
"""Hello"""
Converted to
"\"Hello\""
And not one of these instead.
\""Hello\""
"\"Hello"\"
This may get you started - I don't have a JSON Access engine on my machine, but would a simple TRANWRD or PRXCHANGE function in Perl do the job? Again, this is basic but maybe??
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.