I have a JSON data like this:
{"payload":"{"custom":{"a":{"hs.dl":"hs:\/\/categories\/Z2lkOi8vc2hvcGlmeS9NZW51SXRlbS81NDM2Nzk0NDczODI=","hs.image":"https:\/\/cms-highstreetapp.imgix.net\/denham\/2023\/08\/0657f839-0045-49b0-ba89-fbde3c74f519\/montage20230818-1-kzy7bm.jpg","hs.body":"Reworked in the colours of the season.","hs.video_720":null,"hs.thumbnail":"https:\/\/cms-highstreetapp.imgix.net\/denham\/2023\/08\/0657f839-0045-49b0-ba89-fbde3c74f519\/montage20230818-1-kzy7bm.jpg","hs.title":"Free Move Chinos","hs.video_480":null,"hs.id":"HS_STUDIO-91428"},"i":"31d443e3-1874-4deb-b475-ac10c850451d"},"aps":{"mutable-content":1,"alert":"Free Move Chinos - Reworked in the colours of the season.","category":"category","sound":"default"}}"}
Currently, I am replacing escaping characters this way:
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(value, 'hs.', ''), '\"', '"'), '"{', "{"), '}"', "}"), '\/', "/"), '\n', ' ') AS value
Just wondering is there any better way to do it in dbt and big query?
EDIT:
I created a function to replace JSON escaping characters, but my whole code gets red color due to characters like "
any help would be appreciate
CREATE TEMP FUNCTION customReplace(value STRING)
RETURNS STRING
LANGUAGE js AS """
try {
value = value.replace(/hs./g, '')
.replace(/\"/g, '"')
.replace(/"{/g, '{')
.replace(/}"/g, '}')
.replace(/\//g, '/')
.replace(/\n/g, ' ');
return value;
} catch (e) {
return null;
}
""";
2
Answers
After hours of working with JavaScript, I finally found the right method to replace special characters:
Consider below approach
You can test it with below example
with output
First row – original unescaped JSON and second row with resulted escaped JSON