Skip to main content

working with JSON

JavaScript Object Notation is a schema-less, text-based representation of structured data, based on key-value pairs. JSON is derived from JavaScript and commonly, but not exclusively, used to exchange information between web clients and web servers through APIs.. 
Today JSON is the format of choice for almost every web service.
Relational databases like PostgreSQL and MySQL now support for storing and querying JSON data. NoSQL databases like MongoDB and Neo4j also support JSON.
Below I'll take a quick look at JSON. Let’s start with an example: 
  “firstName”: “Asiri”,
  “lastName”: “Hewage”,
  “loginCount”: 1,
  “isOnline”: true,
  “worksWith”: [“Pearson”, “Intern”],
  “pets”: [
      “name”: “Kalu”,
      “type”: “Dog”
Above structure clearly defines some attributes of an entity. It includes a first and last name ...etc. The structure may be passed from a server to a web browser or a mobile application, which will then perform some action such as displaying/saving it for later reference.
JSON value typesstrings, numbers, booleans, lists, objects, and null. 

npm Installation

Using npm :

  1. Get node.
  2. npm install -g json
Manual method:
  1. Get the 'json' script. Then put it on your PATH somewhere (it is a single file with no external dependencies). Ex:
     cd ~/bin
     curl -L > json
     chmod 755 json
Then check "json" on your PATH:
$ json --version
json 9.0.0
Why should I use JSON? 
The data was transferred in XML format in old applications, and XML was verbose and difficult to manage in JavaScript. JavaScript already had objects, which are a way of expressing data within the language. JSON was much easier for people to read and for browsers to parse. Soon web developers began to prefer JSON over XML.
Now JSON is the standard for exchanging data between web and mobile clients and back-end services. 

When should I use JSON?

  • If you’re writing software that communicates with a browser or native mobile application, you should use JSON as the data format. 
  • In the case of server-to-server communication JSON isn’t a bad choice.
  • If you’re using NoSQL databases, you’re pretty much stuck with whatever the database gives you. In relational databases that support JSON as a type, a good rule of thumb is to use it as little as possible. 
  • You can expect a performance hit when querying for properties within those JSON objects.
  • Its simple design and flexibility make it easy to read and understand, and in most cases, easy to manipulate in the programming language of your choice. 
  • The lack of a strict schema enables flexibility of the format, but that flexibility sometimes makes it difficult to ensure that you’re reading and writing JSON properly.
  • Choosing XML over JSON when building a new web service is a red flag likely to turn away talent, because it indicates a lack of interested in staying relevant. 

Limits of JSON

Here are the five main limitations: 
  1. No schema. 
  2. Only one number type.
  3. No date type. 
  4. No comments. 
  5. Verbosity. 


Popular posts from this blog

Express Js - එක්ස්ප්‍රස් js වලින් වැඩ ගනිමු

යළුවනේ, අද ම්ං මේ කියන්න යන්නේ තවත් JavaScript Library එකක් ගැන. ඒ Express JS. මේක Node.Js වලටම හදපු Web Framework එකක්.  Express කියන්නේ වෙබ් සහ ජංගම යෙදීම් සඳහා ශක්තිමත් විශේෂාංගයක් සපයන අවම හා නම්‍යශීලී Node.js Web Framework එකක්. ( වෙබ් යෙදුම් රාමුවක්). මේ Framework එකෙන් පරිශීලක අතුරු මුහුනතයි (User Interface) එකයි Back End එකයි එකතු කරන්න API ( Application Programming Interface ) එකක් හදාගන්න පුළුවන්. ඒ කියන්නේ අපි හදන වෙබ් ඇප්ලිකේශන් (Web Application) එකේ පරිශීලකයාට පෙනෙන UI එකයි Server එකේ තියෙන ප්‍රධානම බිස්නස් ලොජික් (Business Logic) එකයි සම්බන්ධ කරන්න පුලුවන් මේ Framework එකෙන්. Node JS : npm පාවිච්චි කරල හරිම පහසුවෙන් අපේ project එකට එකතු කරගන්න පුළුවන්. Project path එකේ console එකක් අරන් එකේ මේ code එක execute කරන්න මුළින්ම. මේකෙන් project එකට අවශ්‍ය Express node module එක ඔයාගේ project root එකේ node-modules එකට එකතු කරනවා. $ npm install express --save දැන් ඔයගේ ප්‍රොජෙක්ට් එකට Express dependencies එකතු වෙලා තියෙන්නේ. දැන් app එකක් හදන්න මෙන්න මේ විදිහට server.

Introducing props

React alloıs us to send data to a component in the same syntax as HTML, using attributes or properties on a component. This is akin to passing the src attribute to an image tag. We can think about the property of the  < img /> tag as a prop we're setting on a component called img . We can access these properties inside a component as this.props . Let's see props in action. Recall, we defined the  < Header /> component as:   When we use the < Header /> component, we placed it in our component as like so: We can pass in our title as a prop as an attribute on the < Header / > by updating the usage of the component setting the attribute called title to some string, like so:  Inside of our component, we can access this title prop from the this.props property in the Header class. Instead of setting the title statically as Timeline in the template, we can replace it with the property passed in.  Now our  < Header />  compone