json has elements too

XML/HTML are centered around elements. An element is something that has an opening tag and a closing tag, or simply has a single self-closing tag (The slash before the closing angle bracket is required in XML and XHTML but not HTML). For example, this has six elements:

<!DOCTYPE html>
    <meta charset="utf-8" />
    <title>Hello, world.</title>
    <p>Hello, world.</p>

In this example each tag name is only used once. The elements are (by order of first appearance) html, head, meta, title, body, p. The DOCTYPE definition is special and is not considered to be an HTML or XML element. The charset part of this document is an attribute.

What does element mean in JSON? Something a bit different. An XML element is a compound data type. A JSON element is part of a compound data type.

If you look at the white box on the right part of json.org detailing the grammar, you can see that it has names for items in JSON’s compound data types, objects and arrays. An item in an object is called a pair, and an item in an array is called an element.

It helps when building APIs for processing data formats to have names for things. The terminology for XML is used in XML tools, and has made them easier to understand. For instance, the jQuery API docs use the terms element and attribute heavily.

I think the JSON community should adopt the terms pair and element and use them more frequently, to enhance understanding of the data. It’s useful to talk about a pair rather than a key and a value, because when you add something to a JavaScript object that’s what your adding. You aren’t adding just a value. A JavaScript array can include multiple copies of the same value, so when you’re removing a single occurrence of a value, it makes more sense to say you’re removing an element than removing a value.

JSON has been popular for years now, in many vital technology communities, but it is still lacking in processing support compared to XML. I think clear terminology is one thing that XML has right, and indeed the JSON spec has clear terminology. What’s lacking is widespread use of the terminology.

3 Replies to “json has elements too”

  1. bonjour, merci pour ces informations.
    Moi j’utilise le JSON pour la première fois et j’ai rencontré des problèmes.

    1. Well, there’s http://json.org/ and it’s available in French. Regrettably I don’t speak French. I tried learning it once.

      If you’d like info on REST API design there’s the API-Craft Google Group https://groups.google.com/forum/?fromgroups#!forum/api-craft

      If you need to communicate between a web server and a browser I suggest giving http://backbonejs.com/ a try.

      If you need to consume someone else’s API, I suggest finding a good REST client library for your server side platform. For node I suggest https://github.com/mikeal/request and for python I suggest http://docs.python-requests.org/en/latest/index.html Or, if you can find a specific API client that’s good, use that.

      Best of luck! – Ben

Leave a Reply to Ben Atkin Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.