?
Solved

valid JSON object from selected form elements

Posted on 2014-07-23
8
Medium Priority
?
504 Views
Last Modified: 2014-07-27
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!
0
Comment
Question by:Glen Gibb
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 40216410
dot at the end
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40216413
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
 

Author Comment

by:Glen Gibb
ID: 40216931
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Glen Gibb
ID: 40217418
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
 
LVL 83

Expert Comment

by:leakim971
ID: 40218348
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
 

Author Comment

by:Glen Gibb
ID: 40220754
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
 

Author Comment

by:Glen Gibb
ID: 40220994
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
 

Author Closing Comment

by:Glen Gibb
ID: 40222638
Tnx for your input.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question