Thursday, October 23, 2014

JSON - The briefest tutorial out there!


This blog post will cover a brief area in JSON. Just like the cool name, it does provide some cool services to the web developer. We will talk what is JSON first before getting it to its uses.

What? & Why?

JSON - JavaScript Object Notation. This is just a form of representing data. People at Oracle says:

"JSON is a text-based data exchange format derived from JavaScript that is used in web services and other connected applications."

I have used JSON in some of my web projects too. Solely for one purpose though, but I know there are a lot of other uses with this. My goal was to get the users country with the IP address he/she logs in from. My only option since I bought cheap hosting was to go with this. Click the following link and see what you get,

http://ip2country.sourceforge.net/ip2c.php?format=JSON

Yeah, thats what I thought => correct information!

We call something like this as a JSON Object. There are only two types of data structures in JSON. One as we know is an Object and the other is an Array.

So there are two things, you could create JSON text or you could get uses out of it. We'll talk how to use JSON text.

The two data structures are like this,

Objects will be enclosed in braces({}) and Arrays will be enclosed in box brackets([]). I am going to go ahead and use the same example in the oracle site.

{
   "firstName": "Duke",
   "lastName": "Java",
   "age": 18,
   "streetAddress": "100 Internet Dr",
   "city": "JavaTown",
   "state": "JA",
   "postalCode": "12345",
   "phoneNumbers": [
      { "Mobile": "111-111-1111" },
      { "Home": "222-222-2222" }
   ]
}
 
So this is a JSON text. I think they are just like a tuple in a Database. A method of delivering data, via a connection. Does that sound firmiliar? XML? Ah yes, this is a lot easier way to do things than XML :)

Going back to the description, Values inside an Object or in an Array are separated with a comma (,) and the identifier and the value is separated by a colon (:). These Identifiers are always Strings and their values can take any one of these: string, number, object, array, true, false, and null

Using

So how do we use the text we just received from the server? Did I say that JSON is a derivative of JavaScript? So when you are going JS way things get much more easy. :D There are tonnes of JSON decoders out there for any web based language. In php its 

$json = file_get_contents('the-web-address');
$expression = json_decode($json);
print_r($expression) 
So if we are doing these in JS, we could actually do like this, I shall get the first example to explain this,
 
var details = {
   "firstName": "Duke",
   "lastName": "Java",
   "age": 18,
   "streetAddress": "100 Internet Dr",
   "city": "JavaTown",
   "state": "JA",
   "postalCode": "12345",
   "phoneNumbers": [
      { "Mobile": "111-111-1111" },
      { "Home": "222-222-2222" }
   ]
}
 
document.write(details.firstName); 

Easy as that :)

So using JSON is very easy because,
  • Easy to generate and parse
  • Language independent
  • Light weight
  • Compact
I just wanted to show you how messy a XML file which returns the Country from IP:

Check this out,
http://ip-api.com/xml 

But it also does get the job done!

No comments:

Post a Comment

Its all about friendly scientific conversation here at One Science Fact. I'd love to hear your thoughts about this blog.

Be sure to check back again because i always reply to every single comment