I want to create a JSON string from a javascript for loop. This is what I tried to do (which gives me something that looks like a JSON string), but it does not work.

var edited = "";
for(var i=1;i<POST.length-1;i++) {
    edited += '"'+POST[i].name+'":"'+POST[i].value+'",';
}

It gives me this:

"type":"empty","name":"email-address","realname":"Email Address","status":"empty","min":"empty","max":"empty","dependson":"empty",

This does not work if I try to convert it into a JSON object later.

link|improve this question

78% accept rate
what's your expected result?? – Praveen Prasad Aug 8 '11 at 7:49
This was my expected result: {"type":"empty","name":"empty","realname":"empty","status":"empty","min":"empty"‌​,"max":"empty","dependson":"empty","matches":"empty"}. Problem solved – Sthe Aug 8 '11 at 8:45
feedback

4 Answers

up vote 1 down vote accepted

Two problems:

  • You want an object, so the JSON string has to start with { and end with }.
  • There is a trailing , which may be recognized as invalid.

It's probably better to use a library, but to correct your code:

  • Change var edited = ""; to var edited = "{"; to start your JSON string with a {
  • Add edited = edited.slice(0, -1); after the for loop to remove the trailing comma.
  • Add edited += "}"; after the previous statement to end your JSON string with a }

Your final code would be:

var edited = "{";
for(var i=1;i<POST.length-1;i++) {
    edited += '"'+POST[i].name+'":"'+POST[i].value+'",';
}
edited = edited.slice(0, -1);
edited += "}";

Again, it's best to use a library (e.g. JSON.stringify) by making an object with a for loop, adding properties by using POST[i].name as a key and POST[i].value as the value, then using the library to convert the object to JSON.

Also, you are starting with index 1 and ending with index POST.length-2, therefore excluding indices 0 (the first value) and POST.length-1 (the last value). Is that what you really want?

link|improve this answer
Thank you it worked :-) – Sthe Aug 8 '11 at 8:42
feedback

Can't you just build up a hash and do toString on the hash? Something like this:

var edited = {};
for(var i=0;i<POST.length-1;i++) {    
    edited[POST[i].name] = POST[i].value;
}

Or maybe JSON.stringify is what you are looking for: http://www.json.org/js.html

link|improve this answer
feedback

You have extra comma in your JSON string. JSON string format: {"JSON": "Hello, World"}

var edited = "{";
for(var i=1;i<POST.length-1;i++) {
    edited += '"'+POST[i].name+'":"'+POST[i].value+'",';
}
// remove last comma:
edited = edited.substring(0, edited.length-1) + "}";
link|improve this answer
feedback
//dummy data
var post=[{name:'name1',value:1},{name:'name2',value:2}];

var json=[];
for(var i=0;i<post.length;i++)
{
    var temp={};
    temp[post[i].name]=post[i].value;
   json.push(temp);
}

var stringJson = JSON.stringify(json);


alert(stringJson );

http://jsfiddle.net/3mYux/

link|improve this answer
Thank you. Im getting an error at json.push({POST[i].name:POST[i].value});. It say Unexpected Identifier. – Sthe Aug 8 '11 at 8:04
edited my answer and created a demo for you!! – Praveen Prasad Aug 8 '11 at 8:12
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.