Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

JSON. This article explains JSON, the JavaScript Object Notification.

1. JSON Introduction

JSON (JavaScript Object Notation) is an independent data exchange format designed for representing simple data structures. JSON is limited to text and numeric values. Binary values are not supported.

JSON was originally derived as a subset of the JavaScript Specification (ECME-Script) and it is therefore directly supported in JavaScript.

Data structures in JSON are based on key / value pairs. The key is a string, the value can be a numerical value, a boolean value (true or false) or an object.

While undefined by the standard you should avoid using duplicate key names. Most JSON frameworks will override duplicate keys with the last value.

2. Example of an JSON

An JSON object is a set of key / value pairs which starts with "{" and ends with "}".

{
  "firstName": "Lars",
  "lastName": "Vogel",
  "address": {
    "street": "Examplestr.",
    "number": "31"
  }
}

Lists are one or more values surrounded by [] and separated by ",".

[
  {
    "firstName": "Lars",
    "lastName": "Vogel",
    "address": {
      "street": "Examplestr.",
      "number": "31"
    }
  },
  {
    "firstName": "Jack",
    "lastName": "Hack",
    "address": {
      "street": "Examplestr.",
      "number": "31"
    }
  }
]

3. Java Script

The following is an example of an JSON object and its usage in JavaScript. You evaluate the JSON via the function eval and can then assign it to an object. This object can then be used in your JavaScript source code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<script type="text/javascript">
<!--Evaluate the object and assign to variables -->
var user = {
  "firstName": "Lars",
  "lastName": "Vogel",
  "address": {
    "street": "Examplestr.",
    "number": "31"
  }
}
<!--Use the object-->
alert(user.firstName + ' lives in street ' + user.address.street);
document.writeln(user.firstName + ' ' +user.lastName);
</script>
</body>
</html>

4. About this website

5. Links and Literature

Not yet listed.

5.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.