Lot of things have been said since Douglas Crockford's presentation at XML 2006...
See my presentation at XML Prague 2013 (presentation, proceedings)
which I'd like to share
Sorry if that was already obvious to you!
By User Jef poskanzer on en.wikipedia (Sears catalog) [Public domain], via Wikimedia Commons
<?xml version="1.0" encoding="UTF-8"?> <anvil reference="acme-5103"> <weight unit="pound">9.5</weight> <composition>best wrought iron</composition> <price currency="USD">.15</price> </anvil>
[ "anvil": { "reference": "acme-5103", "weight": { "unit": "pound", "value": 9.5 }, "composition": "best wrought iron", "price": { "currency": "USD", "value": .15 } } ]
By User Jef poskanzer on en.wikipedia (Sears catalog) [Public domain], via Wikimedia Commons
Because they describe the same object
By User Jef poskanzer on en.wikipedia (Sears catalog) [Public domain], via Wikimedia Commons
[ "anvil": { "reference": "acme-5103", "weight": { "unit": "pound", "value": 9.5 }, "composition": "best wrought iron", "price": { "currency": "USD", "value": .15 } } ]
<?xml version="1.0" encoding="UTF-8"?> <anvil reference="acme-5103"> <weight unit="pound">9.5</weight> <composition>best wrought iron</composition> <price currency="USD">.15</price> </anvil>
JSONGeneric data serialization format |
XMLDocument oriented serialization format |
Comparing JSON and XML is like comparing:
The sensible thing to do is to use JSON to express XML...
[{ "name": "anvil", "attributes": {"reference": "acme-5103"}, "children": [ { "name": "weight", "attributes": {"unit": "pound"}, "children": ["9.5"] }, { "name": "composition", "attributes": {}, "children": ["best wrought iron"] }, { "name": "price", "attributes": {"currency": "USD"}, "children": [".15"] } ] }]
[<p>I can support <a href="http://en.wikipedia.org/wiki/PCDATA"><b>mixed</b> content</a>!</p>]
[{ "name": "p", "attributes": {}, "children": [ "I can support ", { "name": "a", "attributes": {"href": "http://en.wikipedia.org/wiki/PCDATA"}, "children": [ { "name": "b", "attributes": {}, "children": ["mixed"] }, " content" ] }, "!" ] }]
JSON can serialize XML nicely
Other approaches have their own use cases
Use JSON tools to manipulate XML
anvil.attributes["reference"]
Or (when possible):
anvil.attributes.reference
elt=function(name){return function(o) {return o.name==name}}; Object.prototype.elt = function(name){return this.filter(elt(name))}
And:
anvil.children.elt('price')[0].attributes.currency
Assuming
js=JSONSelect.match
We can write:
js('.reference', anvil)
Or (safer):
js(':root > .attributes > .reference', anvil)
A Such a simple idea can't be new!
The purpose of JsonML is to provide a compact format for transporting XML-based markup as JSON which allows it to be losslessly converted back to its original form.
[ "anvil", {"reference": "acme-5103"}, [ "weight", {"unit": "pound"}, "9.5" ], [ "composition", "best wrought iron" ], [ "price", {"currency": "USD"}, ".15" ], ]
A purely client-side JavaScript template powered by JSON and returning valid Document Fragments for DOM insertion.
[{ "attrs": {"reference": "acme-5103"}, "content": [ { "attrs": {"unit": "pound"}, "content": {"text": "9.5"}, "type": "weight" }, { "content": {"text": "best wrought iron"}, "type": "composition" }, { "attrs": {"currency": "USD"}, "content": {"text": ".15"}, "type": "price" } ], "type": "anvil" }
JSON |
XML |
XML |
JSON |
Use a spacebar or arrow keys to navigate