HISE Logo Forum
    • Categories
    • Register
    • Login

    remove HTML from JSON

    Scheduled Pinned Locked Moved General Questions
    11 Posts 5 Posters 1.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan KorneffD
      Dan Korneff
      last edited by

      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 - Producer / Mixer / Audio Nerd

      ustkU 1 Reply Last reply Reply Quote 0
      • ustkU
        ustk @Dan Korneff
        last edited by ustk

        @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 or types

        Can't help pressing F5 in the forum...

        Christoph HartC 1 Reply Last reply Reply Quote 0
        • Christoph HartC
          Christoph Hart @ustk
          last edited by

          @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).

          d.healeyD ustkU 2 Replies Last reply Reply Quote 0
          • d.healeyD
            d.healey @Christoph Hart
            last edited by

            @Christoph-Hart

            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.

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            Christoph HartC CasmatC 2 Replies Last reply Reply Quote 0
            • Christoph HartC
              Christoph Hart @d.healey
              last edited by

              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 with ** and let the markdown parser do its magic.

              Dan KorneffD 1 Reply Last reply Reply Quote 1
              • Dan KorneffD
                Dan Korneff
                last edited by

                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 - Producer / Mixer / Audio Nerd

                d.healeyD 1 Reply Last reply Reply Quote 1
                • d.healeyD
                  d.healey @Dan Korneff
                  last edited by

                  @Dan-Korneff Use an inline function and replace the vars with locals

                  for (var i = 0; i < inputString.length; i++)

                  You don't need to declare the i variable like in JavaScript.

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 1
                  • Dan KorneffD
                    Dan Korneff @Christoph Hart
                    last edited by

                    @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."
                    

                    Dan Korneff - Producer / Mixer / Audio Nerd

                    1 Reply Last reply Reply Quote 0
                    • ustkU
                      ustk @Christoph Hart
                      last edited by

                      @Christoph-Hart So we’re talking regex here? Ok guys… pimples are coming on my face already… Have to leave the thread… 😅

                      Can't help pressing F5 in the forum...

                      d.healeyD 1 Reply Last reply Reply Quote 0
                      • d.healeyD
                        d.healey @ustk
                        last edited by

                        @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

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        1 Reply Last reply Reply Quote 2
                        • CasmatC
                          Casmat @d.healey
                          last edited by

                          @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

                          i make music

                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post

                          17

                          Online

                          1.7k

                          Users

                          11.8k

                          Topics

                          102.4k

                          Posts