Skip to content

Instantly share code, notes, and snippets.

@ochafik
Created March 15, 2022 11:31
Show Gist options
  • Select an option

  • Save ochafik/c0c4bbf14d442122c159c17f37fd96ba to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/c0c4bbf14d442122c159c17f37fd96ba to your computer and use it in GitHub Desktop.
URL encode of json
function encodeLength(n) {
return String.fromCharCode('A'.charCodeAt(0) + n);
}
function urlEncodeJspbLite(json, fragments = [], prefix="") {
for (const n in json) {
const key = prefix + (n.length != 1 ? encodeLength(n.length) : '') + n;
const value = json[n];
if (typeof value === 'object') {
urlEncodeJspbLite(value, fragments, key);
} else {
fragments.push(`${key}=${encodeURIComponent(value)}`);
}
}
return fragments;
}
console.log(urlEncodeJspbLite({"2":1,"3":{"1":0,"2":"ca-pub-456"},"11":{"3":1}}).join('&'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment