1

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.

2
  • what's your expected result?? Commented Aug 8, 2011 at 7:49
  • 1
    This was my expected result: {"type":"empty","name":"empty","realname":"empty","status":"empty","min":"empty","max":"empty","dependson":"empty","matches":"empty"}. Problem solved Commented Aug 8, 2011 at 8:45

4 Answers 4

6

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?

0
4
//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/

1
  • Thank you. Im getting an error at json.push({POST[i].name:POST[i].value});. It say Unexpected Identifier. Commented Aug 8, 2011 at 8:04
0

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) + "}";
0

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

Your Answer

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.