In https://www.json.org/, it said:
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
My question is:
Why json is designed to use two types of data structures instead of one ? (name/value pairs only)
For instance,
I use the following two methods to describe the School structure (School contains multiple Students):
1.
{
"Student" : [
{ "Name" : "Peter", "Sex" : "Male" },
{ "Name" : "Linda", "Sex" : "Female" },
]
}
2.
{
"Student" : {
"0" : { "Name" : "Peter", "Sex" : "Male" },
"1" : { "Name" : "Linda", "Sex" : "Female" },
}
}
Which is better?
I like the second one.
Why?
Because in my opinion, the member "Student" is array or map, ordered or unordered, bounded or unbounded, should be defined in its meta data instead of instance data.
The two school json data above are both instance data! (note: the symbol '[]' represent "type infomation" which should be defined in meta data. It's redundant now...)
When I use XPath like syntax to access "Name" member in the example above, they are no different:
- School.Student[0].Name <---[0] as array index
- School.Student[0].Name <---[0] as map key
The same example in XML which has only one way to express:
<School>
<Student Name="Peter" Sex="Male"/>
<Student Name="Linda" Sex="Female"/>
</School>
I heard someone claiming that XML is redundant because of it's ending tags and attributes (relative to s-expression).
But I think the redundancy of XML is only in grammar, the redundancy of Json is in semantic.
Am I right? Very thanks.