Loading ...
Sorry, an error occurred while loading the content.

Re: Non-unique keys in JSON objects

Expand Messages
  • douglascrockford
    ... It should have been MUST. I said SHOULD because I wanted JavaScript s eval function to conform. At the time, I thought that was a necessary transitional
    Message 1 of 6 , Sep 5, 2012
    • 0 Attachment
      --- In json@yahoogroups.com, Matěj Cepl <mcepl@...> wrote:
      >
      > Looking at http://www.ietf.org/rfc/rfc4627.txt I see in the section 2.2
      >
      > An object structure is represented as a pair of curly brackets
      > surrounding zero or more name/value pairs (or members). A name is a
      > string. A single colon comes after each name, separating the name
      > from the value. A single comma separates a value from a following
      > name. The names within an object SHOULD be unique.
      >
      > And that makes me really puzzled. SHOULD???

      It should have been MUST. I said SHOULD because I wanted JavaScript's eval function to conform. At the time, I thought that was a necessary transitional device. Fortunately, it appears that all other implementations, including ES5's JSON.parse, assumed MUST.
    • Jonathan Wallace
      Libjson will parse them all, if you attempt to get the object by its key and said key is not unique, it will give you the first one. The only way to get the
      Message 2 of 6 , Sep 5, 2012
      • 0 Attachment
        Libjson will parse them all, if you attempt to get the object by its key and said key is not unique, it will give you the first one. The only way to get the second would be to use an integrator and check the keys one by one. I've had a few requests to throw exceptions in the case of multiple objects with the same key, but it would break the standard.

        Sent from my iPhone

        On Sep 5, 2012, at 11:13, Matěj Cepl <mcepl@...> wrote:

        > Looking at http://www.ietf.org/rfc/rfc4627.txt I see in the section 2.2
        >
        > An object structure is represented as a pair of curly brackets
        > surrounding zero or more name/value pairs (or members). A name is a
        > string. A single colon comes after each name, separating the name
        > from the value. A single comma separates a value from a following
        > name. The names within an object SHOULD be unique.
        >
        > And that makes me really puzzled. SHOULD??? AFAIK, all languages
        > containing JSON parsers I know about use for the storage of resulting
        > objects data types which require keys to be unique for the instance of
        > the object. At the best Java HashMaps, Python dicts, and JavaScript
        > objects (to name just the languages I am barely familiar with) silently
        > rewrite the old value of the item with the same key.
        >
        > Should is defined in RFC2119 as
        >
        > 3. SHOULD This word, or the adjective "RECOMMENDED", mean that there
        > may exist valid reasons in particular circumstances to ignore a
        > particular item, but the full implications must be understood and
        > carefully weighed before choosing a different course.
        >
        > Does it mean that this SHOULD is in JSON RFC meant as a trap for unawary
        > (because they were supposed to "understand and carefully weigh" full
        > implications of duplicate keys?
        >
        > What am I missing?
        >
        > How is the standard-compliant JSON parser supposed to deal with
        > non-unique keys?
        >
        > Best,
        >
        > Matěj
        > --
        > http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
        > GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC
        >
        > Create like it's 1790.
        > -- Lawrence Lessig
        >
        >

        [Non-text portions of this message have been removed]
      • Matěj Cepl
        ... Brownie points for the discovery should go to my colleague Richard Opálka (ropalka at the same domain my email comes from). Just to make this clear.
        Message 3 of 6 , Sep 5, 2012
        • 0 Attachment
          On 05/09/12 18:16, douglascrockford wrote:
          >> And that makes me really puzzled. SHOULD???
          >
          > It should have been MUST. I said SHOULD because I wanted JavaScript's
          > eval function to conform. At the time, I thought that was a necessary
          > transitional device. Fortunately, it appears that all other
          > implementations, including ES5's JSON.parse, assumed MUST.

          Brownie points for the discovery should go to my colleague Richard
          Opálka (ropalka at the same domain my email comes from).

          Just to make this clear.

          Matěj

          --
          http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
          GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

          I didn't attend the funeral, but I sent a nice letter saying
          I approved of it.
          -- Mark Twain
        • douglascrockford
          ... You can do as you like, but I think it would be better if you throw the exception.
          Message 4 of 6 , Sep 5, 2012
          • 0 Attachment
            --- In json@yahoogroups.com, Jonathan Wallace <ninja9578@...> wrote:

            > Libjson will parse them all, if you attempt to get the object by its key and said key is not unique, it will give you the first one. The only way to get the second would be to use an integrator and check the keys one by one. I've had a few requests to throw exceptions in the case of multiple objects with the same key, but it would break the standard.


            You can do as you like, but I think it would be better if you throw the exception.
          • Jonathan Wallace
            I will likely make it a compilation error in the next release Sent from my iPhone ... [Non-text portions of this message have been removed]
            Message 5 of 6 , Sep 6, 2012
            • 0 Attachment
              I will likely make it a compilation error in the next release

              Sent from my iPhone

              On Sep 5, 2012, at 12:28, "douglascrockford" <douglas@...> wrote:

              > --- In json@yahoogroups.com, Jonathan Wallace <ninja9578@...> wrote:
              >
              > > Libjson will parse them all, if you attempt to get the object by its key and said key is not unique, it will give you the first one. The only way to get the second would be to use an integrator and check the keys one by one. I've had a few requests to throw exceptions in the case of multiple objects with the same key, but it would break the standard.
              >
              > You can do as you like, but I think it would be better if you throw the exception.
              >
              >
              >
              >
              >
              >
              >

              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.