Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
convert URL search params to a JSON object in JavaScript

let str = "AAA=001&BBB=002&CCC=003&DDD=004&EEE=005&FFF=006&GGG=007&HHH=008&III=009&JJJ=010&KKK=011&LLL=012&MMM=013&NNN=014&OOO=015&PPP=016&QQQ=017&QQQ=018"

and I need str to return like this:

{ "AAA":"001", "BBB":"002", "CCC":"003", "DDD":"004", "EEE":"005", "FFF":"006", "GGG":"007", "HHH":"008", "III":"009", "JJJ":"010", "KKK":"011", "LLL":"012", "MMM":"013", "NNN":"014", "OOO":"015", "PPP":"016", "QQQ":"017", "QQQ":"018" }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

here are the answers I reached and both works...

The first solution:

var str = "AAA=001&BBB=002&CCC=003&DDD=004&EEE=005&FFF=006&GGG=007&HHH=008&III=009&JJJ=010&KKK=011&LLL=012&MMM=013&NNN=014&OOO=015&PPP=016&QQQ=017&RRR=018";

const URLSearchParams2JSON_1 = (STRING) => {
    var searchParams = new URLSearchParams(str);
    return Object.fromEntries([...searchParams]);
}

console.log(URLSearchParams2JSON_1(str));

The second solution:

var str = "AAA=001&BBB=002&CCC=003&DDD=004&EEE=005&FFF=006&GGG=007&HHH=008&III=009&JJJ=010&KKK=011&LLL=012&MMM=013&NNN=014&OOO=015&PPP=016&QQQ=017&RRR=018";

const URLSearchParams2JSON_2 = (STRING) => {
    var searchParams = new URLSearchParams(STRING);
    var object = {};
    searchParams.forEach((value, key) => {
        var keys = key.split('.'),
            last = keys.pop();
        keys.reduce((r, a) => r[a] = r[a] || {}, object)[last] = value;
    });
    return object;
}

console.log(URLSearchParams2JSON_2(str));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!