What is the difference between braces "{}" and brackets "[]"?

12

The JSON file format uses two types of symbols to organize the data in its structure, they are:

  • Brackets: [ ]
  • Keys: { }
  • In them you can enter values of several types, see this example example:

    {
      "Nome": "Ana",
      "Endereco": [
                    {"Rua": "Rua da boiada"}, 
                    {"Cidade": "Cruzeiro"}
                  ],
      "Telefones": ["31991188", "39999100"]
    }
    

    My doubts are linked to these symbols mentioned above.

    Questions

    I'd like to know what are the differences between brackets [ ] and { } braces and what would be the appropriate situations for each of them?     

    asked by anonymous 28.12.2016 / 15:30

    3 answers

    16
    • {} are used to create / reference objects
    • [] are used to create / reference arrays

    Basically it can be said that arrays are a list of elements, ordered without specific keys. Objects are a group of elements where each can be referenced by its key.

    Objects and Arrays are types in JavaScript, but they also have representation in other languages. Object can also be called "associative array" because each value has a key associated with it.

    Each of these types have their own methods, which one to use depends on what you need to save / organize.

    If it is necessary to have a list of names, or sequence of numbers and the order in which they are, is important, then you must use an array. You can have objects inside each array. In the objects it is practical to be able to call by key values of the same, so the organization is by keys and not by specific order.

    Analyzing your example would look like this:

    {
      "Nome": "Ana",
      "Endereco": [
                    {"Rua": "Rua da boiada"}, 
                    {"Cidade": "Cruzeiro"}
                  ],
      "Telefones": ["31991188", "39999100"]
    }
    

    Your JSON is an object with 3 keys: Nome , Enderenço , Telefones .

    The Nome key has a String with the value Ana . The Endereço key has an Array as value, this Array consists of Objects. The Telefones key has an Array of strings.

        
    28.12.2016 / 15:32
    7

    Keys { } delimit the structure of an object or what properties / characteristics it will have.

    Brackets [ ] Indicates that there is more than one element, it's the same array notation.

    In the example of the question your object has three properties Nome , Endereco and Telefones ..

    Endereco has a collection (array) of other objects {"Rua": "Rua da boiada"}, {"Cidade": "Cruzeiro"}

    Already Telefones has / points only to a simple array.

        
    28.12.2016 / 15:33
    6

    [] is used to create indexed arrays (that is, they work with numbers), each item being a new index within the array would be almost the same as Array()

    The {} is an object, it looks a bit like array , but it is not, the items inside it are associated with names (keys), it does not preserve the order that the items were added. It would be almost the same as new Object

        
    28.12.2016 / 15:36