19

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.

Ravi Yadav
  • 637
  • 1
  • 9
  • 21
  • You will get best knowledge on this link https://www.json.org/ – Ankur Shah Jan 10 '19 at 06:36
  • 5
    `JSON.stringify` returns a *JSON string*, but "JSON" alone just means the textual format to represent data. You could also stream JSON data or put it in a file, where I wouldn't call it a "string". – Bergi Jan 10 '19 at 06:37
  • 3
    Well Yes it is. Everyone will say "It is a standard." . But it is a string . As simple as that. – Thanveer Shah Jan 10 '19 at 06:39
  • A string is the type that text maps to in JavaScript. So the answer is yes and no. Yes, It's a string because this is how text is represented in JS. No, it's only how it's text is represented in JavaScript, not a universal (although common in other languages). – VLAZ Jan 10 '19 at 06:52
  • 4
    “...send it as JSON” - No, we never send anything “as JSON” since JSON is a standard, hence the word “notation” in the name. Instead, we send them “in JSON” - we encode objects according to the JSON format into strings. – Derek 朕會功夫 Jan 10 '19 at 07:25
  • 1
    JSON is text that adheres to the JavaScript Object Notation standardised format. When passing JSON data, especially over the wire, it's usually encoded as a string. But not all strings are JSON, and not all JSON is encoded as a string. – Dennis Mar 24 '20 at 12:07

5 Answers5

28

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.

Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • 3
    Strictly speaking, referencing a "string" as a data-type (within range of characters considered a 'string' in languages like Java, Python, PHP, etc)... JSON is indeed a String. Prove this by loading into a "String" data type in a programming language (like Java). It works without error and thus conforms to the "string" data type restrictions. Many "standards" like JWE for example are also "strings", base64 is a "string", etc. In JavaScript, JSON.stringify() and JSON.parse() functions transliterate any valid JSON seamlessly, converting the 'string' to an 'object' (in JavaScript) and back again. – Darrell Teague Sep 21 '21 at 13:32
  • JSON is text/string data format and it exists as a string, and it is not a JavaScript object itself. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON#no_really_what_is_json – Maksim Shamihulau May 09 '23 at 09:17
7

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

George
  • 71
  • 1
  • 3
  • This answer is actually the most concise and correct one. Here's a rationale to simplify the discussion: What is or isn't a "string" data type? In most any programming language, that is based on a range of allowable characters. A PNG image cannot be loaded as a "string" unless it is base64-encoded (which is a string). This JSON is a string: {} – Darrell Teague Sep 21 '21 at 13:48
  • Good and convincing answer. – Seyed Abbas Seyedi Nov 14 '21 at 11:14
3

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.
Abhishek
  • 546
  • 5
  • 13
-1

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.

Parit
  • 98
  • 3
  • 1
    JSON is not a subset of YAML. As [json.org](https://www.json.org) says "*It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999.*" – AliN11 Aug 24 '21 at 07:51
  • JSON (JavaScript Object Notation) - is NOT a "language". It is "notation" in string format (keys are strings, delimiter (:) is a string, quotes are parts of strings, values are strings, encapsulation ({}{}) are all strings. It has no logic and thus is not a "language" per se as one might define a "computer language". Much more of a "data structure" in string format (like XML is). – Darrell Teague Sep 21 '21 at 13:36
-2

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.

darth-coder
  • 670
  • 8
  • 20
  • 5
    `JSON.stringify` takes a JavaScript object as input and not a JSON object. There is nothing like a JSONObject in JavaScript , a JavaScript object can hold functions as values, can have circular reference, defined properties, ... and these things can’t be represented in JSON. – t.niese Jan 10 '19 at 06:44
  • 2
    There is no "programmable JSON". You are talking about a JavaScript object. JSON is a textual serialisation of that. You *don't* work with JSON when you have `obj = {name: "Fred"}` in JS. Which will then circle back and explain why "JSON Object" is a misnomer. You're talking about a textual serialised data....object? It makes less sense than a "PIN number" as that is merely a tautology, while a "JSON object" is two actively different concepts passed off into one. It's an oxymoron. – VLAZ Jan 10 '19 at 06:58
  • 3
    JSON is definitely not a data structure. It’s a serialization format. This is like saying CSV is a data structure, which it clearly isn’t. – Derek 朕會功夫 Jan 10 '19 at 07:29
  • A fair point that (in JavaScript) an object is not (just) a string (JSON on the wire). However the rest as pointed out in comments is very much arguable. JSON can exist and be perfectly valid as a plain-text MIME type, validated with no conversion. It is a text-based data structure, much like CSV is a "text based data structure" - just with different delimiters (though admittedly JSON is much richer supporting nesting, some data types, etc). – Darrell Teague Sep 21 '21 at 13:40