Pass a Json object as URL parameter

Posted on October 14, 2021

When you need to pass a more structured set of parameters through URL, here the way to go:

const myObject = {title: 'Some Title', public: true, id: 100}

const obStr = JSON.stringify(myObject)

const param = btoa(obStr)

Then, when you get the param from URL, you can decode this way:

// domain.com/?p=ahgdgf6hhbfdf...

const param = location.search.split('?p=')[1]
let myObject = null
try {
  const obStr = atob(param)
  myObject = JSON.parse(obStr)
} catch (e) {
  return err('Something wrong')
}

console.log(myObject)