Stringify and Parse

Stringify and Parse

Lesson Details:
November 30, 2020


I: Introduction

A: Json (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

A: json is based on two structures: a collection of name/value pairs and an ordered list of values. In both cases, the names are strings.

A: Although json is derived from the JavaScript scripting language, it is language-independent. There are no inherently “javascript” features in json.

A: section A.1 gives a quick introduction to json by showing how to use it in a javascript program. This is followed by a detailed specification in section A.2.

A: Section B gives examples of various ways to represent simple data structures in json.

A: The final sections give some recommendations for reading and writing json in a variety of situations.

II: Body

A: Stringify and parse

A: Suppose we have a name-value pair where the name is “FirstName” and the value is “John”. This might be encoded as follows:

var person = { "FirstName": "John", "LastName": "Doe" };

A: Now suppose we want to convert this structure to a string that can be stored in a file or sent across a network connection. We could do this by evaluating the variable person in the context of the surrounding environment, with the result being a string that looks like this:

{ "FirstName": "John", "LastName": "Doe" }

A: On the other hand, suppose we receive a string that looks like this:

{ "FirstName": "John", "LastName": "Doe" }

A: We can recover the original name/value pair by evaluating it in the context of the surrounding environment. The result is the same object as before, namely, { “FirstName” : “John”, “LastName” : “Doe” } .

III: Conclusion

loader
Course content