Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normal form submission vs. JSON

Tags:

json

post

I see advantages of getting JSON response and formatting in client side but are there any advantages by using JSON for form submission compared to normal submission?

like image 468
Stackee007 Avatar asked Aug 20 '12 17:08

Stackee007


People also ask

How is form data different from JSON?

The main benefit of json over formdata is fields nesting! With json, you can nest fields as you want(dunno if there is a limit), but with formdata, you have to manually stringify the fields first and then add them as a string to the key that would own that nested object.

Can I send form data as JSON?

Using the JSON. stringify() method then format the plain form data as JSON. Specify the HTTP request method as POST and using the header field of the Fetch API specify that you are sending a JSON body request and accepting JSON responses back. Then set the request body as JSON created from the form fields.

What is JSON style documents?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

Is JSON ordered or unordered?

Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine): An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.


2 Answers

One thing comes to mind when dealing with POST data is useless repetition:

For example, in POST we have this:

partners[]=Apple&partners[]=Microsoft&partners[]=Activision 

We can actually see, that there is a lot of repetition here, where as when we are sending JSON:

{"partners":["Apple","Microsoft","Activision"]} 

59 characters versus 47. In this small sample it looks miniscule, but these savings could go up and up, and even several bytes will save you some data. Of course, there is the parsing of data on server side, which could even out the differences, but still, i saw this sometimes helping when dealing with slower connections (looking at you, 3G and EDGE).

like image 174
fosron Avatar answered Oct 10 '22 20:10

fosron


I didn't see any mention of file submission so I thought I'd chime in. FormData seems to be the standard way of submitting files to a server over AJAX. I didn't come across any solutions using JSON but there might be a way to serialize/deserialize files ...

like image 44
J. Barca Avatar answered Oct 10 '22 21:10

J. Barca