0

I'm quite new to programming so I apologize if the answer to my question is obvious:

I need to pass data between MySQL and an iOS app. I'm using php as the go between. The query result that I get via php I'm just passing to my app as a comma-separated/new line separated (comma for new column, new line for new row of data).

I keep reading about JSON and I've read (stackoverflow question on why json and it's associated links) to try and figure out why I would convert my php output into JSON format and then deserialize? on my app side. I keep reading how JSON is very light weight etc. but when I look at it, it seems like I would end up sending so much more data.

ex. if I'm sending some vehicle data:

JSON for 2 vehicles: [{type:'car',wheeles:4,wings:'no'},{type:'plane',wheeles:24,wings:'yes'}]

Same info in csv: car,4,no[/n] plane,24,yes

Of course there are no headers in the csv, but I know that the info will come as type,wheels,wings sending it again and again I would think the total number of bits sent would be a lot more.

My questions are: 1. Would sending the CSV be faster than the JSON string (I think the answer is yes, but would like to hear from the Pros) 2. Given that it is faster and I know the order the data is coming in, is there any reason I should still choose JSON over CSV (some form of robustness of the data as JSON vs. CSV or something else)?

Community
  • 1
  • 1
janson
  • 683
  • 1
  • 5
  • 14
  • 2
    These standardized formats exist for a reason. Your system will break if you ever try to send new-lines or commas as part of your data. – jeroen Apr 02 '15 at 08:12
  • 1
    @jeroen: To be sure, proper CSV (as opposed to homemade `split` solutions) also has methods for handling newlines, commas and quotes. However, the main thing is: CSV represents a table (with a fixed structure) and JSON a tree (with heterogeneous structure). You can't have two kinds of data in a CSV. And it is standard, and natively supported by pretty much everything now (whereas proper CSV is not). And when you see random JSON, it is pretty obvious what it is, but when you see random CSV, you have to know what it represented. – Amadan Apr 02 '15 at 08:16
  • @Amadan Yep, there is definitely more to it :-) – jeroen Apr 02 '15 at 08:17
  • Also regarding volume, sure, JSON is more verbose, but you can gzip your server responses, which will make the size difference a non-issue, given the amount of repetition in a structured JSON. – Amadan Apr 02 '15 at 08:18
  • I'm inexperienced and from what I've seen I kind of "know" that I should be using JSON but I kept thinking, but could not come up with a concrete reason to do so. The fact that the return can be zipped (I did not know this) will hopefully make the size difference a non issue. – janson Apr 02 '15 at 09:15
  • I would like clarification (if it's not too much trouble) on Amadan: "You can't have two kinds of data in a CVS". Do you mean it in the sense that if I send 1.0,Hello,-5 in essence it's just a string? And you cannot have things like arrays or booleans in a CVS? – janson Apr 02 '15 at 09:18
  • I accidentally came back to this question. You can notify people if you tag them like this: @janson (You don't need to do it if you are trying to notify the questioner, or the answerer in comments to an answer), To your question: What I meant was you can't have both cars and car owners in the same document, for example. All rows must have the same structure. What you noted, that everything in CSV is a string, is also correct, but presumably you will know the structure and the reader will do the appropriate decoding. It is not nice, but not a big deal.) – Amadan Apr 05 '15 at 00:54

1 Answers1

2

Would sending the CSV be faster than the JSON string (I think the answer is yes, but would like to hear from the Pros)

Given that particular data structure: Yes, but it is unlikely to be significantly faster. Especially if you use gzip compression at the HTTP level.

If profiling showed that the transfer times were the cause of a significant slow down (unlikely!), then you could always send arrays of data instead of objects.

Given that it is faster and I know the order the data is coming in, is there any reason I should still choose JSON over CSV (some form of robustness of the data as JSON vs. CSV or something else)?

  • JSON is properly standardised. CSV isn't (there are some common conventions and it can mostly be decoded reliably, but edge cases can be problematic).
  • JSON encoders and decoders are widely available and highly compatible with each other.
  • A JSON based format can be, to some extent, self-documenting which makes maintenance of the code that deals with it easier.
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335