Is JSON a string?

Viewed 13948

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.

5 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.

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().in javascript*/

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 properties are not 
quoted anymore like in JSON string above. */
console.log(b["name"]); //this would display HELLO.

JSON is not string

its a language for data exchange between multiple domains, JSON is basically a subset of YAML, that is also a way of exchange data between parties.

Data exchange: Data exchange is the process of taking data structured under a source schema and transforming it into data structured under a target schema so that the target data is an accurate representation of the source data. For transforming data, definitely, you need a parser where you can justify whether the data schema is correct or not for a computer program.

From the context of data - JSON is not a string. It represents data in Key-Value pairs. It follows it's own validation strategy. It has it's own set of rules.

If your context is about how it's transmitted over the network, HTTP usually converts it to a raw string as specified by @samuel-toh.

Even in your code (if you're using let's say Javascript) you can convert it to a string by calling:

JSON.stringify(yourJSONObject);

And can convert it back to programmable Javascript Object by calling:

JSON.parse(stringifiedJSON);

So to answer your question:

No, JSON is not a string. It's a data structure.

EDIT: Please don't confuse between Javascript Object and JSON. They're different. The methods I have specified above take Javascript Object as parameter. What I am trying to imply in my answer is JSON is a language agnostic data-interchange format. (This is not my statement, it's found here.)

There are several differences in JSON and Javascript Object, like @t-niese pointed out, Javascript Objects can have functions as values. And a valid Javascript Object can be an invalid JSON, although a valid JSON will be a valid Javascript Object. Pardon me if I created any confusion.

Related