Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JSON a string?

My question is, is JSON technically a string? I understand that data is passed over the internet via text format. So, text format means string? I had an interview wherein I slipped in that JSON is basically a string and I literally got blasted over it. Is text format not string? We always stringify the object and send it as JSON right? So, wont it make JSON a string?

I couldn't find any clear answers on google stating that JSON is a string. Everywhere its said that it is a text-format.

like image 298
Ravi Yadav Avatar asked Jan 10 '19 06:01

Ravi Yadav


3 Answers

Q: Is JSON a string?

A: No. It is a standard.

We however transmit this format through encoded or raw string over the http protocol, then using API like JSON.parse to create this representation back as key-value paired objects within a process's memory.

like image 91
Samuel Toh Avatar answered Sep 28 '22 01:09

Samuel Toh


JSON is a text-based data format following JavaScript object syntax. JSON exists as a string — useful when you want to transmit data across a network. It needs to be converted to a native JavaScript object when you want to access the data.

This information is taken from the MDN documention, please see it for reference: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON https://www.w3schools.com/js/js_json_intro.asp

like image 43
George Avatar answered Sep 28 '22 01:09

George


JSON is just a format and neither a string nor an object. Normally, JSON string is used to exchange data.However to access data,we have to parse or convert the JSON string into a javascript object.

It would be more clear if we look this concept from code itself. The code below is for javascript as of now,although we can use JSON in other languages as well.

/*Lets say server want to send the variable 'a' which is a 
 JSON String*/
a = ‘{“name”:”HELLO”, “age”: 2000}’;

/*When a client want to use this data,first it will have to 
  convert 'JSON string' into an object.We can do that using 
  JSON.parse().*/

b = JSON.parse(a);

/*Now b become an object and now it is ready to be used and it 
looks like: {“name”:”HELLO”,“age”:2000}. Notice it is not quoted 
anymore like in JSON string above. */
console.log(b["name"]); //this would display HELLO.
like image 30
Abhishek Avatar answered Sep 28 '22 03:09

Abhishek