Difference between revisions of "W2271 JSON FILES"
Dalton-hirst (talk | contribs) |
Dalton-hirst (talk | contribs) |
||
Line 8: | Line 8: | ||
Converting JSON to Javascript is quite easy. In fact, there are a series of Javascript functions that can be called to parse JSON into a Javascript object. | Converting JSON to Javascript is quite easy. In fact, there are a series of Javascript functions that can be called to parse JSON into a Javascript object. | ||
With '''JSON.parse()''', for instance, developers can input a JSON string and | With '''JSON.parse()''', for instance, developers can input a JSON string and parse the text into a Javascript object. | ||
<syntaxhighlight lang="JSON"> | <syntaxhighlight lang="JSON"> | ||
Line 14: | Line 14: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Using the '''JSON.parse()''' function, | Using the '''JSON.parse()''' function, one can simply pass the JSON to the function as follows: | ||
<syntaxhighlight lang="JSON"> | <syntaxhighlight lang="JSON"> | ||
Line 20: | Line 20: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
By doing so, '''JSON.parse()''' will create a Javascript object, in this case a const type object called obj to how the JSON values in a Javascript JSON format. | By doing so, '''JSON.parse()''' will create a Javascript object, in this case, a '''const''' type object called obj to how the JSON values in a Javascript JSON format. | ||
JSON.stringify() | Next, using the '''JSON.stringify()''' function, the object created via the JSON text using '''JSON.parse()''' can be converted to a string. | ||
<syntaxhighlight lang="JSON"> | |||
const obj = {name: "John", age: 30, city: "New York"}; | |||
</syntaxhighlight> | |||
Again, results in a '''const''' type object with the given values. And now, that object can be converted to a string. | |||
<syntaxhighlight lang="JSON"> | |||
const myJSON = JSON.stringify(obj); | |||
</syntaxhighlight> |
Revision as of 19:16, 17 November 2021
What is JSON?[edit]
JSON or Javascript Object Notation is a text-based language-independent data-interchange format very similar to XML or YAML. Although JSON can be used with almost every major coding language, JSON wouldn’t be correctly classified as a “programming language” per se, rather it’s a text-based open standard method of storing, sending, and retrieving data between a client and server. JSON is a very important data representation format, as it is widely used to facilitate the majority of web-based API calls and is very common in facilitating communication between servers and clients in a wide array of applications.
What is the motivation for learning JSON?[edit]
There are several reasons learning JSON can be beneficial for developers, especially those early in their coding career. First off, learning JSON gives developers an easy path to working with a multitude of web-based applications and services that are written in Javascript. Since JSON is a superset of JavaScript, anything you write in JSON can be interpreted by JaveScript compliers, lowering the barrier for entry to working with Javascript-based applications and services. Furthermore, since JSON is a text-only data-interchange format, it is intuitive and the intention of a snippet of JSON can be interpreted easily. Further, nearly every major coding language today has a JSON library allowing JSON to be injected and interpreted by compliers independent of the coding language you wish to write in.
Using JSON with JavaScript[edit]
Converting JSON to Javascript is quite easy. In fact, there are a series of Javascript functions that can be called to parse JSON into a Javascript object.
With JSON.parse(), for instance, developers can input a JSON string and parse the text into a Javascript object.
'{"name":"John", "age":30, "city":"New York"}'
Using the JSON.parse() function, one can simply pass the JSON to the function as follows:
const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
By doing so, JSON.parse() will create a Javascript object, in this case, a const type object called obj to how the JSON values in a Javascript JSON format.
Next, using the JSON.stringify() function, the object created via the JSON text using JSON.parse() can be converted to a string.
const obj = {name: "John", age: 30, city: "New York"};
Again, results in a const type object with the given values. And now, that object can be converted to a string.
const myJSON = JSON.stringify(obj);