DEV Community

Cover image for Learn JSON from Scratch
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on • Updated on

Learn JSON from Scratch

What is JSON?🤔

  • JSON (JavaScript Object Notation) is a lightweight data-interchange format.

  • JSON is easy for humans to read and write, yet it is efficient enough for computers to handle.

  • JSON is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, or passing state information between the two) and for serializing structured data into a string representation.

  • It can be used to represent numbers, strings, booleans, arrays, objects, and raw bytes in a standardized way.

  • JSON is often used to output data in the Web API's response body.

Advantages of using JSON

  • JSON is an open standard that enables data to be exchanged between applications without the need for a middleman.

  • JSON is easy for humans to read and write, which makes it ideal for data exchange between different applications.

  • JSON is more compact than XML, and therefore easier to transmit.

  • It is supported by all programming languages.

  • It can be used on all platforms.

  • It is a light weight database standard, so data transmission is fast.

Where JSON is used?

  • JSON is used as the data format for many web services and APIs, such as GitHub, Netflix, Twitter and Facebook

  • Mobile Applications

  • Gaming API's

Data types supported

  1. String
  2. Number
  3. Arrays
  4. Null
  5. Boolean
  6. Object

JSON methods in JavaScript

  1. JSON.parse(): Takes JSON string and convert into JavaScript object.

  2. JSON.stringify(): Convert JavaScript object JSON into JSON string useful while sending over the network.

Syntax and Example

{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 1000000,
      "secretIdentity": "Unknown",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Connect with Me 😊

🔗 Links

linkedin

twitter

Top comments (4)

Collapse
 
aarone4 profile image
Aaron Reese

Not that I am particularly a fan of XML which is probably the second most popular data transfer mechanism, but JSON is missing a few key elements that make working with XML easier
1) no ability to add annotations and comments. JSON is all data; this is changing with the JSON5 standard but it is not widely adopted and is a breaking change
2) no data type definitions. With XML you can publish a DTD document that specifies what nodes are required, optional, repeatable etc. You can then map this to the target document and validate that it is in the right format
3) no automated transformation. With XML you can also create an XLST file to automatically transform the incoming structure to another structure; removing and combining nodes, reordering data, adding aggregate values etc.

I'm not saying don't use JSON just be aware it has some limitations and there are other options

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Hi, Aaron Reese
Thanks for sharing your knowledge.

let's talk about XML disadvantages

Like any good technology, XML does not try to be the solution to every problem.

There are some things it just cannot do well, and it would be foolish to try to force it to do them.

The foremost requirement of XML is that it be universally accessible, a lowest common denominator for applications. This necessarily throws out many optimizations that are necessary in some situations.

Let’s review some of these limitations:

1.

  • XML is not optimized for access speed. XML documents are meant to be completely loaded, and then used as a data source.
  • The parser is required to do a syntax check every time it reads in the markup.
  • In contrast, modern databases are optimized for quick data lookups and updates.

2.

  • XML is not compact.
  • There is no official scheme for compressing XML.
  • XML parsers expect uncompressed text.
  • You either have to put up with large text files, or you have to create a complex mechanism for compressing and decompressing on the fly, which will add to your processing overhead.
  • Most proprietary applications, like Microsoft Word and Adobe FrameMaker, save data in a binary format by default, saving disk space and perhaps speeding up file transfers.
  • HTTP offers compression for transmission, which helps reduce this cost.
  • Many kinds of data are not suited for embedded markup.
  • XML is most useful for text data with a hierarchical structure.
  • It does not offer much for binary data.
  • For example, raster graphic images are long streams of binary data, unreadable to anyone until passed.

In fact, I'm not a fan of XML or JSON
I'm a fan of any technology to do what I want to build with it.

Here you and I explain what is XML limitations and what is JSON limitations.

Anyone now can choose what is better.

Thanks again

Collapse
 
balajin96 profile image
balaji

thanks

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

You're welcome 😎