-1

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:

  1. School.Student[0].Name <---[0] as array index
  2. 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.

chansey
  • 1,266
  • 9
  • 20
  • In your XML example I see a List rather than a Map, and in your XPath standard array indexation, I don't see how it supports your point. – Aaron Nov 17 '17 at 10:47
  • Also your question might be better received on the [Software Engineering SE](https://softwareengineering.stackexchange.com/) – Aaron Nov 17 '17 at 10:51
  • @Aaron Yes, in the XML example, Student is a list. That is what I want to express. – chansey Nov 17 '17 at 10:52
  • You could store everything in a string and be done with it. – biziclop Nov 17 '17 at 10:52
  • Well without going any deep, you have be able to make a difference between a bunch of objects that may be of the same type but are not directly related to each other and a collection of objects which are of the same base type and belong together in a context, Plus Arrays enforce order, objects do not, Array is fundamental data structure and is present in nearly every language, regardless of whether or not other higher level collections are available. – A4L Nov 17 '17 at 10:54
  • @A4L "Arrays enforce order". Yes, but it is semantic infomation. These semantic infomation should be store in meta data instead of instance data. We may have more data structures not just array eg: array with bound? array but key is string? map also can be look as a kind of array? – chansey Nov 17 '17 at 11:09
  • @A4L IMO, we do not need array structure, what is we need is multiple value structure. – chansey Nov 17 '17 at 11:11
  • @chansey Have you ever implemented a data structure? I mean only for educational purpose, a Map for example? Do you know that it is based on arrays? Do you know how arrays are saved on the RAM and how are elements accessed via index? Please do some research in these regards and maybe there will be a change that you will change your opinion ;-) – A4L Nov 17 '17 at 11:22
  • @A4L Why do you not ask me how to implement array or map in Turing machine? Your reply has nothing to do with my question... – chansey Nov 17 '17 at 12:02

1 Answers1

1

Redundant for what?

Discussing the theories of data transfer? Then yes, it's redundant as the other structure it supports can represent arrays.

Balancing the concerns of sparseness, representational integrity, simplicity for human readers and writers, simplicity for software parsers and generators, speed of parsing and generating, simplicity of mapping to data structures and simplicity of validation — that is to say, the actual design goals — then no. Being able to directly encode arrays has benefits for the sparseness, simplicity of humans, simplicity of validation (if it's a valid array in syntax it's a valid array, which doesn't necessarily follow from maps that could omit indices without a separate application-specific rule to handle that) and speed of producing (if something maps on increasing indices then we are forced to potentially catch someone mixing them up). What is a redundancy in the sense of analysing what can be represented is not redundant in the sense of not offering a practical benefit.

In the second case of "beyond what we need or want" redundant has a rather pejorative connotation while in other cases redundant can be a positive thing (having redundancy gives you room to deal with loss, ability to catch errors and so on in different cases). The redundancy here is of the second sort; we could use a version of JSON that had no arrays, so we don't strictly need it, but life is a lot easier because we do have it and almost nobody is going to go to the effort of producing a horrible mapping of ascending integers to elements just to make the life of those parsing it harder.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • Your answer is very valuable to me. Yes, the "beyond what we need or want" is what I want to say. Thanks a lot! – chansey Nov 17 '17 at 14:39
  • You mentioned sparseness, representational integrity, simplicity for human readers and writers, and so on. But what about "consistency" or "elegant"? If we can use nested Map (only in text not implement) to describe Array, why introduce Array? (eg: s-expression) Another reason is when developing a large project, we usually introduce some metadata file. (eg: XML Schema / JSON Schema) In this case, the semantic of '[]' is actually redundant. (eg: XML has no array, we can define in xsd for multiple value semantic ) Thanks a lot again! – chansey Nov 17 '17 at 14:39
  • What does that theoretical consistency gain us in practice? (Okay, the parsing library could be simpler, though historically one of the first ways to parse it was a raw `eval` which while insecure did work and from which starting point producing an array from the results of that is less simple, not more) Why would we value it in the use-cases it is put to? – Jon Hanna Nov 17 '17 at 16:08
  • Because I want to know the thinking behind json design. For example, why json has array and xml haven't? Why do we usually use xml schema but usually do not use json schema? Should we use json only in javascript projects, and xml in other cases? (json can reduce impedance mismatch in javascript, but not in all languages) Why recent years most people advocate the use of json as common data transfer format instead of xml? – chansey Nov 18 '17 at 02:04