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 9173933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:40:04+00:00 2026-06-17T16:40:04+00:00

I have a name value pair stored as a string that I would like

  • 0

I have a name value pair stored as a string that I would like to convert to an object in jQuery or JavaScript. Here is an example of that pair.

{‘auto’:false, ‘itemAWidth’:20, ‘itemBWidth’:20, ‘marginLeft’:0,
‘marginRight’:0, ‘maximumWidth’:0, ‘propW’:5, ‘propX’:5, ‘propY’:null,
‘propZ’:null}

This is being stored as a string. How can I convert this to a proper object? Some of my unsuccessful attempts appear below.

convertValuePairsToObject = function (pairs) {

  var outObject = {};
  var nodes = pairs.split(',');
  for (var node in nodes) {

  }

  //alert(pairs);
  //return jQuery.parseJSON('"' + pairs + '"');
  //      var outObject = {};
  //      //var nodes = pairs.split(','), dest = outObject;
  //      var  = pairs.split(',');
  //      return outObject;
}

When I attempted to use the jQuery.parseJSON() function, the code returned a very long string that started like this.

{‘0’:'{‘, ‘1’:”’, ‘2’:’0′, ‘3’:”’, ‘4’:’:’, ‘5’:”’, ‘6’:'{‘, ‘7’:”’, ‘8’:’,’, ‘9’:’ ‘, ’10’:”’, ’11’:’1′, ’12’:”’, ’13’:’:’, ’14’:”’, ’15’:”’, ’16’:”’, ’17’:’,’, ’18’:’ ‘, ’19’:”’, ’20’:’2′, ’21’:”’, ’22’:’:’, ’23’:”’, ’24’:’a’, ’25’:”’, ’26’:’,’, ’27’:’ ‘, ’28’:”’, ’29’:’3′, ’30’:”’, ’31’:’:’, ’32’:”’, ’33’:’u’, ’34’:”’, ’35’:’,’, ’36’:’ ‘, ’37’:”’, ’38’:’4′, ’39’:”’, ’40’:’:’, ’41’:”’, ’42’:’t’, ’43’:”’, ’44’:’,’, ’45’:’ ‘, ’46’:”’, ’47’:’5′, ’48’:”’, ’49’:’:’, ’50’:”’, ’51’:’o’,

=====================================

Edit Below 10:51 AM 1/4/2013

=====================================

So I tried the suggestions you both offered, and have this.

  convertObjectToString = function (obj) {
      return JSON.stringify(obj);
      //      var str = '';
      //      for (var p in obj) {
      //         if (obj.hasOwnProperty(p)) { str += "'" + p + "':" + formatValue(obj[p]) + ", "; }
      //      }
      //      if (str.length > 0) {
      //         str = String(str).substring(0, str.length - 2); // Trim trailing comma
      //         str = '{' + str + '}';
      //      }
      //      return str;
   }

   convertValuePairsToObject = function (pairs) {
      return jQuery.parseJSON('[' + pairs + ']');
   }

But now when convertValuePairsToObject executes, the code pre-pends the name value pairs with this:

{“0”:

And if I repeatedly trigger that function, it will keep pre-pending the above to the string, like so:

{“0”:{“0”:{“0”:

I don’t need that index identifier of zero. How can I eliminate that?

  • 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-06-17T16:40:05+00:00Added an answer on June 17, 2026 at 4:40 pm

    assuming you mean that you get the string like this:

    "{'auto':false, 'itemAWidth':20, 'itemBWidth':20, 'marginLeft':0, 'marginRight':0, 'maximumWidth':0, 'propW':5, 'propX':5, 'propY':null, 'propZ':null}"
    

    Then one option would be to use eval like this:

    var pairs = "{'auto':false, 'itemAWidth':20, 'itemBWidth':20, 'marginLeft':0, 'marginRight':0, 'maximumWidth':0, 'propW':5, 'propX':5, 'propY':null, 'propZ':null}"
    
    var foo = eval("(" + pairs + ")");
    

    foo is now the actual object and not the string.

    Executing console.log(foo); will result in the object tree in the debugger not a string.


    DEMO – get the object from the string


    Alternatively you can convert your object in a string into a proper JSON object and then back into an object using JSON.Parse as mentioned by others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a name value pair array in Javascript (just like form.SerializeArray()) which i
I have a table that looks something like this: Name Year Value A 2000
It looks like good JSON objects typically have a name-value pair, with value being
I have an XML file consisting of Name/Value pairs that I need in a
I have a JSON file that has more name value pairs than I care
I have a .txt file like: Symbols from __ctype_tab.o: Name Value Class Type Size
I have a MySQL table structured somewhat like this: type name value ===================== 1
Suppose I have this annotation @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Name { String value(); }
I have a name/value pair in a List<T> and needing to find the best
Below I have table - Company id name value year 1 IBM 10 2011

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.