JSON is rediscovering XML Schema, XML DTDs etc, when we had those a quarter century ago already.
It was so good when you could define the structure easily and validate it with standard tooling.
JSON is rediscovering XML Schema, XML DTDs etc, when we had those a quarter century ago already.
It was so good when you could define the structure easily and validate it with standard tooling.
If I have a structure like this JSON:
{
"foo": "bar",
"nested": {
"nested_foo": "nested_bar",
}
}
Should I do this in XML? <foo>
bar
</foo>
<nested>
<nested_foo>
nested_bar
</nested_foo>
</nested>
Or should I do this? <foo>
bar
</foo>
<nested nested_foo="nested_bar"/>
If your goal is to simply encode a data structure that looks more or less like a C-style struct (key/values, arrays, primitives), the concept of attributes on tags is superfluous, and introduces ambiguity in how to serialize something.To me, JSON is nice because it's essentially the minimum viable way of encoding a C-style struct (floating point behavior notwithstanding.) XML has extras like attributes, schemas, and DTDs, which may be useful, but they come at the cost of having additional syntax (<!DOCTYPE ...>, etc), which is auxiliary to the goal of encoding data structures, and thus makes it no longer as minimal.
To me, JSON's approach of having a separate out-of-band definitions of schema (e.g. JSON schema) is the better approach because it's less opinionated, you only pay for it when you need it, and it doesn't require separate syntax. Leave validation to a validation step, my data is my data.
I see some XML from other sources and it’s just awful. Everything that should be a nested tag is all jammed into an unreadable tag with attributes with no visual structure at all, spilling over the 80th column and word-wrapping in the terminal, it’s just like, what the hell. Why even use XML at that point?
Where you'd do attributes is like this:
<foo type="address">
bar
</foo>
<nested type="resident_list">
<nested_foo type="resident">
nested_bar
</nested_foo>
</nested>
A shitty example (you'd define types with schema), but you get the point.The data is in the tags, attributes are metadata, extra information that's needed during the process.
This is what JSON is lacking, you can't give metadata to data, like defining types. The type definition needs to live NEXT to the data in special keys you need to check every time when parsing.
I had been tasked at least several times in my past jobs to develop XSLT scripts to transform data into user-readable content. I don't know of anyone that uses XSLT today and I have no idea if there is a JSON equivalent.
Though as I understand it it's possible that this might not be the case for much longer: https://github.com/whatwg/html/issues/11523
jq is the closest thing for json, but it’s far messier to write longer filters than XSLT, and that’s saying something!
I was building some kind of parser for them and got confused for a second when my Python script returned bare XML when fetching the front page :D