Google Apps Script:
Working with JSON

How to:

In Google Apps Script, manipulating JSON is a straightforward process, largely due to the native support JavaScript provides for JSON parsing and stringification. Here are some common operations:

1. Parsing JSON: Assume we retrieve a JSON string from a web service; parsing it into a JavaScript object is essential for data manipulation.

var jsonString = '{"name": "Sample Project", "version": "1.0.0"}';
var obj = JSON.parse(jsonString);
Logger.log(obj.name); // Output: Sample Project

2. Stringifying JavaScript Objects: Conversely, converting a JavaScript object to a JSON string is useful when we need to send data from Apps Script to an external service.

var projectData = {
  name: "Sample Project",
  version: "1.0.0"
};
var jsonString = JSON.stringify(projectData);
Logger.log(jsonString); // Output: '{"name":"Sample Project","version":"1.0.0"}'

3. Working with Complex Data: For more complex data structures, such as arrays of objects, the process remains the same, showcasing the flexibility of JSON for data representation.

var projects = [
  {name: "Project 1", version: "1.0"},
  {name: "Project 2", version: "2.0"}
];
var jsonString = JSON.stringify(projects);
Logger.log(jsonString); // Output: '[{"name":"Project 1","version":"1.0"},{"name":"Project 2","version":"2.0"}]'

Deep Dive

JSON’s ubiquity in modern web applications can’t be understated, rooted in its simplicity and how seamlessly it integrates with JavaScript, the language of the web. Its design, inspired by JavaScript object literals, albeit stricter, facilitates its swift adoption. In the early 2000s, JSON gained popularity as an alternative to XML for AJAX-driven web applications, offering a more lightweight and less verbose data interchange format. Given Google Apps Script’s deep integration with various Google APIs and external services, JSON serves as a pivotal format for structuring, transporting, and manipulating data across these platforms.

While JSON reigns supreme for web applications, alternative data formats like YAML for configuration files or Protobuf for more efficient binary serialization in high-performance environments exist. However, JSON’s balance of readability, ease of use, and wide support across programming languages and tools solidifies its position as a default choice for many developers venturing into Google Apps Script and beyond.