Notes on JSON

Aus Weis nix
Wechseln zu: Navigation, Suche

JSON (JavaScript Object Notation) is a lightweight data-interchange format.


  • [1] JSON Homepage
  • [2] PHP JSON Extension

Comparison to XML

Lifted from Ajaxpatterns ...

JSON explicitly defines itself as a "fat-free alternative to XML". JSON and XML share the following properties:

  • Both are formats that depict an object as a plain-text string.
  • Being plain-text string formats, both are suitable for transfer across HTTP. This means that both are suitable as inputs and outputs for a web service.
  • Each format is supported by libraries in numerous languages, including Javascript. There are libraries to convert from native objects to either format and back again to native objects.

JSON has several advantages over XML:

  • JSON is more compact and, for humans, the lack of tags can make it easier for humans to comprehend the underlying data.
  • It's often claimed that JSON is faster for browsers to parse, though a recent investigation suggests XML parsing actually scales better.
  • JSON is a concrete data format. XML, in contrast, is really a meta-format, and a developer has many choices to make about the precise XML dialect to use. Each mapping strategy has its own conventions - for example, developers need to decide on tag names and decide between tag attributes or nested tags. Consequently, a server-side XML-object mapping framework may not be message-compatible with a Javascript counterpart, and never underestimate the amount of meetings and emails that will be necessary to resolve a seemingly trivial argument over data formats. In that sense, JSON is comparable to a well-defined XML dialect rather than XML itself.
  • Within browsers, JSON has wider support and more consistent handling, because it's based on standard Javascript.
  • JSON happens to be quite compatible with YAML, another XML alternative that's gaining traction in the dynamic scripting community.

XML has several advantages over JSON:

  • XML is vastly more familiar to the IT community than JSON.
  • XML is more self-documenting. The header identifies which XML format is being used, and there's often a schema or DTD which defines the format precisely.
  • XML has much more support in terms of libraries and tool support. JSON libraries tend to be simply about conversion, which its advocates would probably argue is all that's required. XML, on the other hand, has support in terms of DTD and Schema validators, XPath tools to interrogate the data, XSLT processors to perform translations, and so on. Furthermore, many IDEs, editors, and debugging environments make XML easy to work with.
  • For any given task, developers usually have the luxury of choosing between several competing implementations.
  • XML has good browser support, too. DOM interrogation, XPath and XSLT, are all possible in modern browsers.

php-json Extension


Version 1.1.0 needs this fix in json_c/json_tokener.c

   case json_tokener_state_number:
     if(!c || !strchr(json_number_chars, c)) {
       int numi;
       double numd;
       int tmp_l;
       char tmp[32];
       tmp_l = (int)(this->pos - start_offset) % 32;
       strncpy( tmp, this->source + start_offset, tmp_l);
       if(!deemed_double && sscanf(tmp, "%d", &numi) == 1) {