valid JSON object from selected form elements

Experts,

I need to evaluate a dynamically-created form of class="required" and submit via Ajax.

I can select and loop those elements, and set focus on the empty ones.  

The difficulty is returning JSON that will parse!   The return tests valid on JSONLint:
 [{"clientType":"1"},{"corpName":"Bannerman Inc"},{"purchaseOrder":"BNMN345"},{"firstName":"Cal"},{"lastName":"Tech"},{"addressType":"1"},{"address2":"22 Rose Bowl"},{"zip":"13566"},{"city":"1"},{"state":"1"},{"country":"1"},{"phone":"483-22"},{"EType":"0"}].

Open in new window


But here's the error:  "Form submitted. Status: parsererror, SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data."

And here's the code:

function validate(id) {
	var content = '#' + id + ' .required';
	var query = []; 
	var element = null;
	var isValid = true; // local var evaluated by $(div_id).function()
	$(content).each(function() {
		element = $(this);
		name = element.attr('name');
		value = element.val();
		isValid = (value!== '' && value !== null);
		if(!isValid) {
			return isValid; // return value to outer loop....
		}
		else
			var item = {};
			item[name] = value;  *** clientType=1 *** How to get {"clientType":"1"} ??
			query.push(item);
	});
	if(!isValid) {
		alert(element.attr('id') + ' is required. ');
		$('#' + element.attr('id')).focus(); 	// focus on invalid element ...
	}
	else
		return JSON.stringify(query);
}

Open in new window

I've researched all sorts of possibilities w/out success.  Help!
Glen GibbOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
dot at the end
0
leakim971PluritechnicianCommented:
if that can help : http://jsfiddle.net/ZcD3R/1/
var j = [{"clientType":"1"},{"corpName":"Bannerman Inc"},{"purchaseOrder":"BNMN345"},{"firstName":"Cal"},{"lastName":"Tech"},{"addressType":"1"},{"address2":"22 Rose Bowl"},{"zip":"13566"},{"city":"1"},{"state":"1"},{"country":"1"},{"phone":"483-22"},{"EType":"0"}];
var jS = '[{"clientType":"1"},{"corpName":"Bannerman Inc"},{"purchaseOrder":"BNMN345"},{"firstName":"Cal"},{"lastName":"Tech"},{"addressType":"1"},{"address2":"22 Rose Bowl"},{"zip":"13566"},{"city":"1"},{"state":"1"},{"country":"1"},{"phone":"483-22"},{"EType":"0"}]';

try {
    JSON.parse(j);
    alert("j ok");
}
catch(e) {
    alert("j bad\n"+e);
}
try {
    JSON.parse(jS);
    alert("jS ok");
}
catch(e) {
    alert("jS bad\n"+e);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Glen GibbOwnerAuthor Commented:
That silly dot had to come from a cut and paste in the middle of a sentence I was composing.

Here's another copy of the output.  Again, JSLint accepts it.

[{"clientType":"1"},{"corpName":"Bannerman Inc"},{"purchaseOrder":"BNMN345"},{"firstName":"Cal"},{"lastName":"Tech"},{"addressType":"1"},{"address2":"22 Rose Bowl"},{"zip":"13566"},{"city":"1"},{"state":"1"},{"country":"1"},{"phone":"483-22"},{"EType":"0"}]
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Glen GibbOwnerAuthor Commented:
You're always right on, Leakim.  

I see that enclosing the output in single quotes produces the parseable json.  But how to arrive at that single quote eludes me -- "'" + query + "'" sure doesn't do it!

How do I compile my query array to output the jS var that you demonstrate?
0
leakim971PluritechnicianCommented:
why do you need to stringify your object? so you need further to use json.parse or jQuery.parseJSON

you may use : JSON.stringify( query );
but it will stringigy the whole object not just adding quotes at the beggining and at the end... maybe this is what you're looking for...
0
Glen GibbOwnerAuthor Commented:
Nope.  I'm just trying to compose a json array that will parse.  I can submit as text, but that seems a bit lame.
0
Glen GibbOwnerAuthor Commented:
Here's the latest source from the attached code:  
{"clientId":"0","clientType":"1","active":0,"dateJoined":0,"lastContact":0,"corpId":"0","corpUserId":0,"corpName":"Bannerman Inc","purchaseOrder":"BNMN345","contactId":0,"usrId":0,"firstName":"Cal","initial":0,"lastName":"Tech","addressId":"0","addressType":"1","address1":0,"address2":"22 Rose Bowl","zip":"13566","cityId":"1","stateId":"1","countryId":"1","e_SvcId":"0","phone":"483-22","email":0,"e_TypeId":"0","data3":0}

Open in new window


Everything gets "stringified" but the final 0!  

Three online evaluations clear it.  But it doesn't parse!

But the error whines about "unexpected end of data at line 1, column 1."  That's a curly brace!

Sorry to be so slow in responding, but I've had other issues lately.  If a few more points would help, I'll close and repost.convert-json.txt
0
Glen GibbOwnerAuthor Commented:
Tnx for your input.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.