I'm not sure you could. For example, in another comment, I mentioned DocBook[1]. How would you do the following sample document in JSON?
<?xml version="1.0" encoding="UTF-8"?>
<book xml:id="simple_book" xmlns="http://docbook.org/ns/docbook" version="5.0">
<title>Very simple book</title>
<chapter xml:id="chapter_1">
<title>Chapter 1</title>
<para>Hello world!</para>
<img src="hello.jpg"/>
<para>I hope that your day is proceeding <emphasis>splendidly</emphasis>!</para>
</chapter>
<chapter xml:id="chapter_2">
<title>Chapter 2</title>
<para>Hello again, world!</para>
</chapter>
</book>
Would you make each <chapter> into an object? But you have 2 <para> children in there with an <img> in between. And one <para> has an additional <emphasis> in the content. I can't think of a good JSON schema equivalent to this. ['book', {'id': '...'},
['title', {}, ...],
['chapter', {'id': 0},
['title', {}, 'Chapter 1'],
...
],
['chapter', {'id': 1},
...
],
]
more realistically, you could just represent it with the AST of that XML, i.e {
'type': 'book',
'attrs': {'id': ...},
'children': [
{
'type': 'title',
'children': ['Simple book']
},
{
'type': 'chapter',
...
},
{
'type': 'chapter',
...
},
...
]
}
so you could do that emphasis bit as [
'this text nees more',
{'type': 'emphasis',
'children': ['emotion']},
'!'
]
hellish to write by hand but probably okay for a program to consume (modulo all the XML libs/tooling you can't use). and you could probably even write some kind of schema for it.if i actually had to represent that data, i'd also move some child nodes into attributes, e.g. make all nodes with 'type': 'book' also have a 'title' attribute, like you would if you had an AST datatype
which is probably the only way to properly deal with markup and especially commented sections that can span over paragraph start/ends - neither JSON or XML seems to have a proper answer for such annotations and I wonder if there's any standard format that can that, especially if humans still want to reasonable be able to view or edit iit...
(OOXML and its binary equivalents more or less solve this by completely separating paragraph and character formatting, both separately indexing the spans of text they annotate)
{
"id": "simple_book",
"title": "Very simple book",
"chapters": [
{
"id": "chapter_1",
"content": [
{ "type": "title", "value": "Chapter 1" },
{
"type": "para",
"content": [
{ "type": "text", "value": "Hello World!" }
]
},
{ "type": "img", "src": "hello.jpg" },
{
"type": "para",
"content": [
{ "type": "text", "value": "I hope that your day is proceeding " },
{ "type": "emphasis", "value": "splendidly" },
{ "type": "text", "value": "!" }
]
}
]
}
]
}This AFAICT was actually why SVG has a few bizarre choices - such as putting all the drawing commands into attributes. A browser that doesn't understand an embedded SVG document in its HTML would be left with just the text contents.
I mean, in theory, you could do this in JSON or some other data structure. But you would go insane and be shooting yourself in the head before long.