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

Question about Arrays in JSON

Expand Messages
  • sean_snider
    When creating a JSON structure, is there a standard way that is used to determine that some member should be an Array vs Not? For someone building a service,
    Message 1 of 2 , Feb 9, 2010
    • 0 Attachment
      When creating a JSON structure, is there a standard way that
      is used to determine that some member should be an Array vs Not?

      For someone building a service, if there is only one item do they
      output an Array? How does one make that decision from the service side?

      Do simply have to follow whatever schema you want, and make all clients aware?

      Do different JSON seriliazation techniqunes handle this in different ways?

      S
    • John Cowan
      ... I don t know that there is. My take on it is: if something *can* appear multiple times, it should always be an array. If it cannot, it should never be an
      Message 2 of 2 , Feb 9, 2010
      • 0 Attachment
        sean_snider scripsit:

        > When creating a JSON structure, is there a standard way that
        > is used to determine that some member should be an Array vs Not?

        I don't know that there is. My take on it is: if something *can*
        appear multiple times, it should always be an array. If it cannot,
        it should never be an array.

        For example, a message might have a sender and one or more recipients.
        Since the sender is unique, it should not be an array. The recipients
        should always be an array, even if there is just one recipient (and even
        if that is the typical case).

        Sticking to this rule makes uniform access easy: a client can always
        iterate over the array rather than having to have a special case in the
        code for a non-array. The cost is only 2 characters on the wire.

        --
        John Cowan <cowan@...> http://ccil.org/~cowan
        Micropayment advocates mistakenly believe that efficient allocation of
        resources is the purpose of markets. Efficiency is a byproduct of market
        systems, not their goal. The reasons markets work are not because users
        have embraced efficiency but because markets are the best place to allow
        users to maximize their preferences, and very often their preferences are
        not for conservation of cheap resources. --Clay Shirkey
      Your message has been successfully submitted and would be delivered to recipients shortly.