978-1-491-92948-3 [LSI] Introduction to JavaScript Object Notation by Lindsay Bassett Copyright © 2015 Lindsay Bassett. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Meg Foley Production Editor: Kristen Brown Copyeditor: Jasmine Kwityn Proofreader: Charles Roumeliotis Indexer: Ellen Troutman Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest August 2015: First Edition Revision History for the First Edition 2015-08-04: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491929483 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Introduction to JavaScript Object Nota‐ tion, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii 1. What Is JSON?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 JSON Is a Data Interchange Format 1 JSON Is Programming Language Independent 2 Key Terms and Concepts 4 2. JSON Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 JSON Is Based on JavaScript Object Literals 5 Name-Value Pairs 6 Proper JSON Syntax 7 Syntax Validation 10 JSON as a Document 11 The JSON MediaType 11 Key Terms and Concepts 11 3. JSON Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Quick Look at Data Types 13 The JSON Data Types 15 The JSON Object Data Type 16 The JSON String Data Type 17 The JSON Number Data Type 19 The JSON Boolean Data Type 20 The JSON Null Data Type 20 The JSON Array Data Type 21 Key Terms and Concepts 26 iii
4. JSON Schema. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Contracts with Validation Magic 30 Introduction to JSON Schema 31 Key Terms and Concepts 36 5. JSON Security Concerns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 A Quick Look at Client- and Server-Side Relationships 39 Cross-Site Request Forgery (CSRF) 40 Injection Attacks 43 Cross-Site Scripting (XSS) 43 Holes in Security: Architectural Decisions 45 Key Terms and Concepts 46 6. The JavaScript XmlHttpRequest and Web APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Web APIs 50 The JavaScript XMLHttpRequest 53 Relationship Woes and Rules About Sharing 57 Cross-Origin Resource Sharing (CORS) 57 JSON-P 58 Key Terms and Concepts 60 7. JSON and Client-Side Frameworks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 jQuery and JSON 64 AngularJS 66 Key Terms and Concepts 71 8. JSON and NoSQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The CouchDB Database 74 The CouchDB API 77 Key Terms and Concepts 85 9. JSON on the Server Side. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Serializing, Deserializing and Requesting JSON 88 ASP.NET 88 PHP 92 A Smorgasbord of JSON HTTP Requests 96 Ruby on Rails 97 Node.js 98 Java 98 Key Terms & Concepts 99 iv | Table of Contents
10. Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 JSON as a Configuration File 101 The Big Picture 104 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Table of Contents | v

Introduction to JavaScript Object Notation, Lindsay Bassett, 2015

  • 2.
    978-1-491-92948-3 [LSI] Introduction to JavaScriptObject Notation by Lindsay Bassett Copyright © 2015 Lindsay Bassett. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Meg Foley Production Editor: Kristen Brown Copyeditor: Jasmine Kwityn Proofreader: Charles Roumeliotis Indexer: Ellen Troutman Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest August 2015: First Edition Revision History for the First Edition 2015-08-04: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491929483 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Introduction to JavaScript Object Nota‐ tion, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
  • 3.
    Table of Contents Preface.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii 1. What Is JSON?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 JSON Is a Data Interchange Format 1 JSON Is Programming Language Independent 2 Key Terms and Concepts 4 2. JSON Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 JSON Is Based on JavaScript Object Literals 5 Name-Value Pairs 6 Proper JSON Syntax 7 Syntax Validation 10 JSON as a Document 11 The JSON MediaType 11 Key Terms and Concepts 11 3. JSON Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Quick Look at Data Types 13 The JSON Data Types 15 The JSON Object Data Type 16 The JSON String Data Type 17 The JSON Number Data Type 19 The JSON Boolean Data Type 20 The JSON Null Data Type 20 The JSON Array Data Type 21 Key Terms and Concepts 26 iii
  • 4.
    4. JSON Schema.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Contracts with Validation Magic 30 Introduction to JSON Schema 31 Key Terms and Concepts 36 5. JSON Security Concerns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 A Quick Look at Client- and Server-Side Relationships 39 Cross-Site Request Forgery (CSRF) 40 Injection Attacks 43 Cross-Site Scripting (XSS) 43 Holes in Security: Architectural Decisions 45 Key Terms and Concepts 46 6. The JavaScript XmlHttpRequest and Web APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Web APIs 50 The JavaScript XMLHttpRequest 53 Relationship Woes and Rules About Sharing 57 Cross-Origin Resource Sharing (CORS) 57 JSON-P 58 Key Terms and Concepts 60 7. JSON and Client-Side Frameworks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 jQuery and JSON 64 AngularJS 66 Key Terms and Concepts 71 8. JSON and NoSQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The CouchDB Database 74 The CouchDB API 77 Key Terms and Concepts 85 9. JSON on the Server Side. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Serializing, Deserializing and Requesting JSON 88 ASP.NET 88 PHP 92 A Smorgasbord of JSON HTTP Requests 96 Ruby on Rails 97 Node.js 98 Java 98 Key Terms & Concepts 99 iv | Table of Contents
  • 5.
    10. Conclusion. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 JSON as a Configuration File 101 The Big Picture 104 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Table of Contents | v