0

I have an object like this:

{ 
 user[id]: 1, 
 user[name]: 'Lorem', 
 money: '15.00' 
}

And I want this:

{ 
 user: { 
 id: 1, 
 name: 'Lorem', 
 }, 
 money: '15.00' 
}

How can I achieve this?

nickhar
21.1k12 gold badges64 silver badges77 bronze badges
asked Jul 30, 2012 at 1:42
2
  • Is the first JSON? Can you make a jsfiddle.net demo of your problem? Commented Jul 30, 2012 at 1:47
  • I want to transform the input values and keys direct in a JSON...jsfiddle.net/MFWLH/1 And vice-versa. Sometimes I have the json and want to set in the inputs.. Commented Jul 30, 2012 at 2:03

1 Answer 1

2

Something like this

var x = { 
 "user[id]": 1, 
 "user[name]": 'Lorem', 
 "money": '15.00' 
}
var y = {};
for (var i in x){
 var z = /(.+)\[(.+)\]/g.exec(i);
 if (z){
 if (!y.hasOwnProperty(z[1])){
 y[z[1]] = {};
 }
 y[z[1]][z[2]] = x[i]; 
 }
 else{
 y[i] = x[i];
 }
}

http://jsfiddle.net/mowglisanu/3Z2qe/

answered Jul 30, 2012 at 2:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.