DEV Community

Cover image for What is JSON ?
Himanshu Baghel
Himanshu Baghel

Posted on

What is JSON ?

The full name of JSON is JavaScript Object Notation.JSON is a lightweight format for storing and transporting data and it is text-based so it can be easily read by human. JSON became a data Exchange standard on web.
JSON data represented in the form of key and value pair and curly braces {} holds data as object.

'{"name":"John", "age":30, "city":Delhi}'
Enter fullscreen mode Exit fullscreen mode

In above code name,age and city are the keys and John,30 and delhi are the values of that keys.

let personName = obj.name;
let personCity = obj.city;
Enter fullscreen mode Exit fullscreen mode

Using above code we can access JSON data.

Datatype of Values-
In JSON, values must be one of the following types:

  • string
  • number
  • object
  • array
  • boolean
  • null

*Note :- JSON Syntax is subset of Javascript.

In the past we use XML for data Exchange but now JSON uses increase tremendously specialy in Javascript.

Another exam of JSON file

{"employees":[
  { "firstName":"John", "lastName":"Doe" },
  { "firstName":"Anna", "lastName":"Smith" },
  { "firstName":"Peter", "lastName":"Jones" }
]}
Enter fullscreen mode Exit fullscreen mode

Above JSON code employees is key and it's value is in the form of array and here are 3 element of array and each element is again an object and hold keys and value.

XML file for same data looks like-

<employees>
  <employee>
    <firstName>John</firstName> <lastName>Doe</lastName>
  </employee>
  <employee>
    <firstName>Anna</firstName> <lastName>Smith</lastName>
  </employee>
  <employee>
    <firstName>Peter</firstName> <lastName>Jones</lastName>
  </employee>
</employees>
Enter fullscreen mode Exit fullscreen mode

We use XML because, XML is much more difficult to parse than JSON and JSON is parsed into a ready-to-use JavaScript object easily.

Thank You...

Top comments (6)

Collapse
 
ravavyr profile image
Ravavyr

A good explanation of JSON, however your final statement is not right.

XML is just as easy to parse as JSON:
With Javascript: w3schools.com/xml/xml_parser.asp
With PHP: w3schools.com/php/php_xml_simplexm...

XML is less efficient because it generates a larger file [opening and closing tags <...> take up more bytes that squigglies {...}
XML is also just seen as "the old way of doing things" as pretty much almost everything new is built with JSON objects.
Nowadays i see only older .NET based APIs that still use XML, that and some RSS feeds, though many of those have converted to JSON feeds too.
So there really is no benefit to using XML over JSON other than, it's what you're used to, which in itself isn't really a problem.

Personally, I prefer a comma-separated list without keys and using [] and |s to split objects. The keys are then hardcoded since you never use an object without knowing the keys you need to use/render/parse, and it means the data i send is always organized to match the code (no giant json object and you only use 3 fields from it). My method in turn uses even less bandwidth and processing time AND makes the raw data harder to understand for a potential hacker since the keys are not present in the data.
I do know this does not work for every scenario, but think about it.

I started using JSON nearly 10 years ago just because it became the dominant way of requesting data from endpoints/APIs/ajax calls/fetch requests (they're all the same thing regardless of what names people keep making up for them)
I don't really know WHY it became prominent, but i know that every newbie coming in is learning JSON and not XML.

Collapse
 
himanshubaghel07 profile image
Himanshu Baghel

Thank you Ravavyr for correcting me and I also noted that what are you explain. I appreciate you

Collapse
 
tracygjg profile image
Tracy Gilmore

Hi Himanshu,
Did you know JSON has an ECMA specification like JavaScript. ECMA-404.
Regards, Tracy

Collapse
 
himanshubaghel07 profile image
Himanshu Baghel

Yes, I know about ECMA specification but not know more about it and I must check out ECMA-404 link you share with me. Thank you Tracy

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
himanshubaghel07 profile image
Himanshu Baghel

Thank you Abhay for your appreciation