3

I'm working with an app that connects to a PHP/MySQL server from which everything is returned in JSON format. For example, a list of users is returned as a JSONArray of JSONObject. Each object contains the individual user's information (name, location, phone number, etc).

In working with information in this format is is more efficient to leave everything in JSON format and only extract items from the array/objects as needed? Or is it better to extract everything from the JSONArrayand included objects to build a regular Array or ArrayList first?

3
  • I really like the JSONArray and other JSON classes and recommend them for ease of use, but I have no idea about performance or efficiency. I'll be curious to see the answers - good question. Commented Jan 17, 2013 at 16:51
  • Can you be clear than this? It's always better to have a JSONArray of JSONObjects having each json object contains user's information in the format of key/value like name/Rarw Commented Jan 17, 2013 at 16:55
  • I can try (thought I thought that question was pretty clear). I'm trying to figure out which is more efficient (1) leaving data in JSON format and extracting it as needed or (2) extracting things from the JSON objects and putting regular strings into an array or some other collector. I guess it comes down to knowing whether there is more or less overhead in parsing the JSON vs a regular array or list. Commented Jan 17, 2013 at 17:59

2 Answers 2

3

JSONArray internally uses ArrayList. It's just wrapper over ArrayList. So I'd say there is no difference. JSONObject uses HashMap so again no real drawbacks.

Sign up to request clarification or add additional context in comments.

3 Comments

If its just a wrapper isn't that going to be slower? I'm assuming there's some time to parse the JSON vs a regular string. So then parsing a JSONArray, which is wrapping an ArrayList, would be less efficient than just getting entires from an ArrayList
@rarw parsing will be done anyway regardless whether you use list or jsonarray. Wrapping introduces some delays but they are really small - method invocation very fast in java.
Ok - so then the real answer is that JSON will be slightly less efficient than using regualr string values. However, at lest in most cases the difference will be negligable (unless you're using a massive set of data in which case the wrapping process has to eat up more time/memory).
0

In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects

  • JSON is smaller than corresponding XML
  • JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)

JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse and JSON.stringify when not supported by browsers.

While eval at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.

JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.

While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.

I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.

1 Comment

Nice answer, but wrong question. This question didn't ask anything about XML. :)

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.