remove HTML from JSON
-
I'm looking to display messages coming from my webserver. The JSON response contains HTML data and is displayed as a string:
"message": "<strong>Error:</strong> The username <strong>undefined</strong> is not registered on this site.",
Is there an easy way to strip HTML from this string?
-
@Dan-Korneff maybe simply
json.message = json.message.replace("<strong>", "").replace("</strong>", "");
And if there are more tags a routine can be written to remove any <tag> or </tag> types
-
@ustk I think that might be a use case for a RegEx replacement (IIRC David already had something like this, otherwise you should just be able to google "how to remove HTML tags with Regex" and work your way through the syntax hell of Regex until you're satisfied with the result).
-
IIRC David already had something like this
I thought I had too, but I can't find it... maybe I was enquiring about it but didn't pursue it.
-
But if the tags that this will spit out are known then you‘re maybe better off with a manual replace (in this case you could even replace <strong> with
**
and let the markdown parser do its magic. -
I worked out a primitive function to get the job done.
function removeHtmlTagsFromString(inputString) { var stringWithoutTags = ""; var withinTag = false; for (var i = 0; i < inputString.length; i++) { var char = inputString.charAt(i); if (char === "<") { withinTag = true; continue; } if (char === ">") { withinTag = false; continue; } if (!withinTag) { stringWithoutTags += char; } } return stringWithoutTags; } const var stringWithTags = "<strong>Error:</strong> The username <strong>undefined</strong> is not registered on this site."; var stringWithoutTags = removeHtmlTagsFromString(stringWithTags); Console.print(stringWithoutTags); //Output is "Error: The username undefined is not registered on this site."
-
@Dan-Korneff Use an inline function and replace the
vars
withlocals
for (var i = 0; i < inputString.length; i++)
You don't need to declare the
i
variable like in JavaScript. -
@Christoph-Hart @d-healey great ideas!
Updated script:inline function replaceHtmlTagsFromString(inputString, replacement) { local stringWithReplacedTags = ""; local withinTag = false; for (i = 0; i < inputString.length; i++) { local char = inputString.charAt(i); if (char === "<") { withinTag = true; stringWithReplacedTags += replacement; continue; } if (char === ">") { withinTag = false; continue; } if (!withinTag) { stringWithReplacedTags += char; } } return stringWithReplacedTags; } // Example usage: const var stringWithTags = "<strong>Error:</strong> The username <strong>undefined</strong> is not registered on this site."; var stringWithReplacedTags = replaceHtmlTagsFromString(stringWithTags, "**"); Console.print(stringWithReplacedTags); // Output: "**Error:** The username **undefined** is not registered on this site."
-
@Christoph-Hart So we’re talking regex here? Ok guys… pimples are coming on my face already… Have to leave the thread…
-
@ustk said in remove HTML from JSON:
@Christoph-Hart So we’re talking regex here? Ok guys… pimples are coming on my face already… Have to leave the thread…
When I have a trig problem I ask Greg, when I have a regex problem I ask ChatGPT
-
@d-healey couple weeks late, but heres it in case anyone needs it:
https://forum.hise.audio/topic/6308/hise-regex-is-wierd-how-to-select-html-tags/7