wetlip
wetlip

Reputation: 133

Is JSON the Object itself or the String of an Object?

The question speaks for itself. JSON isn't a type. You have the function JSON.stringify(object). My opinion is that the resulting String from this function is JSON, not the object which is stringified. And if that is true JSON would be a String ?

Upvotes: 1

Views: 197

Answers (3)

Weedoze
Weedoze

Reputation: 13943

JSON(JavaScript Object Notation) is a file-format that uses human-readable text.

In the following snippet, you can see that I created a JavaScript object (object) then I used JSON.stringify(object) to get the JSON version (string). typeof is used to show you the type of the element next to it.

typeof object will give you the type of object

You can also notice that there are some changes. For example, properties names are wrapped with double quotes " ", the values are wrapped with double quotes instead of simple,...

const object = {
  propertyA : "This is my value A",
  propertyB : [1,2,3],
  propertyC : {test: 'Nice'}
};

console.log(typeof object);
console.log(JSON.stringify(object));
console.log(typeof JSON.stringify(object));

Upvotes: 1

S Jayesh
S Jayesh

Reputation: 201

JSON is a Objects Notation (serialised), but JSON has specifice characteristics that it can be converted into/from specific syntax of String (for usage into different languages/platforms or for data transfer into different systems).

In javaScript :

to convert syntactically_correct_string to JSON we use JSON.parse(inputStringHere),

and to convert a JSON into String we use JSON.stringify(inputJSONobject).

Upvotes: 0

Clonkex
Clonkex

Reputation: 3637

JSON (JavaScript Object Notation) is a way to format object data as a string. You are correct in saying JSON is not a type. JSON.stringify(object) takes an object and returns a string. The string will contain the data of the object, but in a human-readable form.

So JSON is the string of an object.

Upvotes: 7

Related Questions