Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Proper decoding using Vanilla Javascript

Proper decoding using Vanilla Javascript

Scheduled Pinned Locked Moved JavaScript
javascriptphpcomtoolsjson
5 Posts 2 Posters 0 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I have this cookie that I wrote in PHP, and I need to read the cookie to get the Project Number. I tried using decodeUri but it strips the semi colon out of the JSON. I also plagurized some JavaScript to get the cookie in the first place. If I use this [URL Decoder/Encoder](https://meyerweb.com/eric/tools/dencoder/) it gives me the proper decoding that I need. It looks like URL encoding to me. cookie: {"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}

    function getCookie(name) {

    let cookie = {};
    document.cookie.split(';').forEach(function(el) {
        let \[k,v\] = el.split('=');
        cookie\[k.trim()\] = v;
    })
    return cookie\[name\];
    

    }

    If it ain't broke don't fix it Discover my world at jkirkerx.com

    R 1 Reply Last reply
    0
    • J jkirkerx

      I have this cookie that I wrote in PHP, and I need to read the cookie to get the Project Number. I tried using decodeUri but it strips the semi colon out of the JSON. I also plagurized some JavaScript to get the cookie in the first place. If I use this [URL Decoder/Encoder](https://meyerweb.com/eric/tools/dencoder/) it gives me the proper decoding that I need. It looks like URL encoding to me. cookie: {"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}

      function getCookie(name) {

      let cookie = {};
      document.cookie.split(';').forEach(function(el) {
          let \[k,v\] = el.split('=');
          cookie\[k.trim()\] = v;
      })
      return cookie\[name\];
      

      }

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      How are you generating that cookie value? It looks like URL-encoded JSON, but the JSON is invalid.

      "{\"timeStamp\":\"02/17/2022 10:15:02\",\"coreMode\":\"open\",\"}"

      That trailing quote before the closing brace doesn't match an opening quote. Attempting to parse the value will give you an error:

      const value = JSON.parse(decodeURIComponent('{"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}'));
      // JSON.parse: unterminated string at line 1 column 56 of the JSON data


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      J 1 Reply Last reply
      0
      • R Richard Deeming

        How are you generating that cookie value? It looks like URL-encoded JSON, but the JSON is invalid.

        "{\"timeStamp\":\"02/17/2022 10:15:02\",\"coreMode\":\"open\",\"}"

        That trailing quote before the closing brace doesn't match an opening quote. Attempting to parse the value will give you an error:

        const value = JSON.parse(decodeURIComponent('{"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}'));
        // JSON.parse: unterminated string at line 1 column 56 of the JSON data


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        I generated that cookie with PHP 7.4. For some reason it encodes the JSON, but it reads clean with PHP 7.4 I pulled that example out of storage in the browser, and cut off a large section just to use as an example. This is the cookie straight out of the browser storage. Pretty ugly but that's just how it writes even with pretty print. %7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D The same cookie URL decoded ... {"timeStamp":"02\/18\/2022 09:41:35","coreMode":"open","projectIntegrityCheck":false,"projectNumber":"5342","projectType":"poolspa","projectStage":"designing","projectVersion":0,"projectFinalVersion":0,"projectJob":0,"projectEmployeeId":91,"projectEmployeeName":"Jim Miller","projectCustomerId":"5324","projectCustomerName":"Jim Miller 2022021602","projectAddNumber":-1,"projectSalesId":"91","originalBookPrice":32761.2153,"bookPrice":32761.2153,"preBookPrice":32761.2153,"contractAmount":0,"inputId":"","inputName":"Selection Name:","inputPrice":0,"inputMode":"ADD","addTotal":0,"bookDifference":0,"userName":"jimk","userType":"Executive","versionNumber":-1,"versionBookPrice":32761.2153,"salesCommission":0} Maybe reading this cookie with JavaScript is not a good idea. I found that I don't really need this since my projectClose.php will read the cookie and close the project.

        If it ain't broke don't fix it Discover my world at jkirkerx.com

        R 1 Reply Last reply
        0
        • J jkirkerx

          I generated that cookie with PHP 7.4. For some reason it encodes the JSON, but it reads clean with PHP 7.4 I pulled that example out of storage in the browser, and cut off a large section just to use as an example. This is the cookie straight out of the browser storage. Pretty ugly but that's just how it writes even with pretty print. %7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D The same cookie URL decoded ... {"timeStamp":"02\/18\/2022 09:41:35","coreMode":"open","projectIntegrityCheck":false,"projectNumber":"5342","projectType":"poolspa","projectStage":"designing","projectVersion":0,"projectFinalVersion":0,"projectJob":0,"projectEmployeeId":91,"projectEmployeeName":"Jim Miller","projectCustomerId":"5324","projectCustomerName":"Jim Miller 2022021602","projectAddNumber":-1,"projectSalesId":"91","originalBookPrice":32761.2153,"bookPrice":32761.2153,"preBookPrice":32761.2153,"contractAmount":0,"inputId":"","inputName":"Selection Name:","inputPrice":0,"inputMode":"ADD","addTotal":0,"bookDifference":0,"userName":"jimk","userType":"Executive","versionNumber":-1,"versionBookPrice":32761.2153,"salesCommission":0} Maybe reading this cookie with JavaScript is not a good idea. I found that I don't really need this since my projectClose.php will read the cookie and close the project.

          If it ain't broke don't fix it Discover my world at jkirkerx.com

          R Offline
          R Offline
          Richard Deeming
          wrote on last edited by
          #4

          Given that string, JSON.parse and decodeURIComponent work fine:

          const parsedCookie = JSON.parse(decodeURIComponent("%7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D"));

          /*
          { timeStamp: "02/18/2022 09:41:35", coreMode: "open", projectIntegrityCheck: false, projectNumber: "5342", ...
          */

          I can't see a semi-colon which would be stripped out. :confused:


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          J 1 Reply Last reply
          0
          • R Richard Deeming

            Given that string, JSON.parse and decodeURIComponent work fine:

            const parsedCookie = JSON.parse(decodeURIComponent("%7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D"));

            /*
            { timeStamp: "02/18/2022 09:41:35", coreMode: "open", projectIntegrityCheck: false, projectNumber: "5342", ...
            */

            I can't see a semi-colon which would be stripped out. :confused:


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            decodeUriComponent did not occur to me. I'll go ahead and give that a try and finish the module. I may post another question soon about communicating between browser tabs using LocalStorage or opening up some sort of channel communication. The goal is for an inactivity timer on the main browser tab to close other open tabs that feed off the same cookie, so when the cookie is wiped, the other open tabs won't bomb from the missing cookie.

            If it ain't broke don't fix it Discover my world at jkirkerx.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups