Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:46:53+00:00 2026-05-27T17:46:53+00:00

I have a string like this: abc=foo&def=%5Basf%5D&xyz=5 How can I convert it into a

  • 0

I have a string like this:

abc=foo&def=%5Basf%5D&xyz=5

How can I convert it into a JavaScript object like this?

{
  abc: 'foo',
  def: '[asf]',
  xyz: 5
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:46:54+00:00Added an answer on May 27, 2026 at 5:46 pm

    In the year 2021… Please consider this obsolete.

    Edit

    This edit improves and explains the answer based on the comments.

    var search = location.search.substring(1);
    JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}')
    

    Example

    Parse abc=foo&def=%5Basf%5D&xyz=5 in five steps:

    • decodeURI: abc=foo&def=[asf]&xyz=5
    • Escape quotes: same, as there are no quotes
    • Replace &: abc=foo","def=[asf]","xyz=5
    • Replace =: abc":"foo","def":"[asf]","xyz":"5
    • Suround with curlies and quotes: {"abc":"foo","def":"[asf]","xyz":"5"}

    which is legal JSON.

    An improved solution allows for more characters in the search string. It uses a reviver function for URI decoding:

    var search = location.search.substring(1);
    JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) })
    

    Example

    search = "abc=foo&def=%5Basf%5D&xyz=5&foo=b%3Dar";
    

    gives

    Object {abc: "foo", def: "[asf]", xyz: "5", foo: "b=ar"}
    

    Original answer

    A one-liner:

    JSON.parse('{"' + decodeURI("abc=foo&def=%5Basf%5D&xyz=5".replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have an XML String like this, <METHOD>foo</METHOD> <PARAM1>abc</PARAM1> <PARAM2>def</PARAM2> ... <PARAM99>ghi</PARAM99> <PARAM100>jkl</PARAM100>
I have a string that looks like this: abc[1,2,3].something.here,foo[10,6,34].somethingelse.here,def[1,2].another I want to split this
A Tcl nested String can be something like this: {abc {xyz foo {hello world}}}
I have code like this: string result = xml.Root.Descendants(XYZ).Descendants(ABC).Descendants(MNO).Single().Value; 1) Is there a better
I have a string like this: String a = 'ABC(rfg) OR DEF(3dsgf56737(fg)sdg) OR KLM(dfgrg)
I have a string like this: SOMETHING='abc.abc.abc' How can I extract the content of
I have a string like this: var tempSting:String = abc@abc.com;xyz@xyz.com I want to add
I have a string that contains something like this: ##### abc 'foo' /path/to/filename:1 #####
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I have a string like this that I need to parse into a 2D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.