Search code examples
google-apps-scriptdecodeunescapestring

Decode/Unescape Unicode Google App Scripts


I am new to decoding hence I am unable to decode below response while using UrlFetchApp.

Below is the function I am using to fetch data but getting values in unicode which I need to decode. Any function that I can use to decode this?

function scrape() {
  
  var url = UrlFetchApp.fetch("https://immi.homeaffairs.gov.au/visas/working-in-australia/skillselect/invitation-rounds");
  var elements =/id="ctl00_PlaceHolderMain_PageSchemaHiddenField_Input" (.*?)\/>/gim;
  
  var x = url.getContentText().match(elements);
  
  Logger.log(x);
  
}

Solution

  • Although I'm not sure whether this is the best way, how about this modification?

    Modified script:

    function scrape() {
      var url = UrlFetchApp.fetch("https://immi.homeaffairs.gov.au/visas/working-in-australia/skillselect/invitation-rounds");
      var elements =/id="ctl00_PlaceHolderMain_PageSchemaHiddenField_Input" (.*?)\/>/gim;
      var x = url.getContentText().match(elements);
    
      var res = unescape(x[0].replace(/\\u/g, "%u")); // Added
    
      Logger.log(res)
    }
    

    Result:

    When above modified script is used, as a sample, the values are converted as follows.

    From:
    \u003cp\u003eThe table below shows the number of invitations issued in the SkillSelect invitation round on 11 September 2018.\u003c/p\u003e\n\n\u003ch3\u003eInvitations issued on 11 September 2018\u003c/h3\u003e\n\n
    
    To:
    <p>The table below shows the number of invitations issued in the SkillSelect invitation round on 11 September 2018.</p>\n\n<h3>Invitations issued on 11 September 2018</h3>\n\n
    

    References:

    If I misunderstand your question, I'm sorry.