Chani
Chani

Reputation: 5165

how do JSON help a web developer?

how do JSON help a web developer ? is it better than conventional methods ? if yes ; then in which way ? where can i learn JSON properly? what type of project/scenario is most suitable for JSON ? is it suitable for ruby-on-rails ?? can i use it to work with MongoDB ???

Upvotes: 1

Views: 250

Answers (2)

Samo
Samo

Reputation: 8240

It depends on how you want to use it. Is your app a web service or a website? If it's a website, then JSON is probably most useful for your AJAX calls. It will return an object that's very easy to work with. Consider this JavaScript object:

var person = {
  lastName: "Doe",
  firstName: "John"
}

You can interact with this object very easily.

person.lastName // "Doe"
person.firstName // "John"

This is the same with a JSON object returned by a controller.

To accomplish this in a Rails app:

// someFile.js
var success = function(response) {
  // Iterate over the object's properties
  for (var property in response) {
    // Show the values of properties that were not inherited
    if (response.hasOwnProperty(property)) {
      alert(property);
    }
}

$.get("/someController/some_action/" + some_id, success);

# SomeController.rb
def some_action
  @obj = SomeClass.find(params[:some_id])
  respond_to do |format|
    format.js { render :json => @obj }
  end
end

If you're doing a web service, JSON is a good way to go pretty much all the time, but your clients may be requesting different content types, so I recommend you support JSON as well as xml and any other content type they may request. Some clients may even request xhtml, in which case your web service and your website wouldn't be much different :)

Upvotes: 2

Luca Matteis
Luca Matteis

Reputation: 29267

I love JSON. I think it's a very good format not only because it's humanly-readable and easily interpretable by a machine, but it's also the native notation used by the language of the web - JavaScript.

how do JSON help a web developer ?

Well, for a developer used to JavaScript it helps because there's no extra learning curve. For others, it helps in a way that is simple and concise and tons of libraries to work with it, documentation and community support.

is it better than conventional methods ?

From a performance point of view, it actually is. You can Google for "JSON performance" to get more information on the subject. It's also better in a way that it's simpler, and very easy to read for a human.

where can i learn JSON properly?

Well a good starting point would be where the standard is located: http://json.org

can i use it to work with MongoDB ?

Yes you can as MongoDB manages collections of JSON-like documents.

Upvotes: 3

Related Questions