50

I am new to json. Some json examples i have seen have data within the curly braces and some json examples have subdata within square brackets.

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}
}

From http://json.org/example.html

What is the need/purpose of having data within the square brackets?

regards

jijinp
  • 2,592
  • 1
  • 13
  • 15
Uswer721
  • 565
  • 1
  • 7
  • 13
  • 4
    @Quentin my question differed from the question suggested as duplicate. – Uswer721 Apr 18 '16 at 08:31
  • No, it doesn't. You are asking what the difference between `[]` and `{}` is, that question is asking what the difference between `[]`, `{}` and `new Array()` is. That's as close to identical as makes no practical difference. – Quentin Apr 18 '16 at 08:37
  • @Quentin Maybe. I thought [ ] **within** { } had a different significance. No problemo, i got it now. – Uswer721 Apr 18 '16 at 08:56
  • 5
    The distinction of using a JSON example was important to me. I suppose it's ok to be marked a dupe as long as the question remains available. – MrBoJangles Jan 22 '18 at 20:29
  • 9
    What a weak reason for something to be a duplicate... I see where that's coming from, but that assumes you know that javascript and json are practically the same thing, otherwise it will seem odd. – Broots Waymb Feb 09 '18 at 20:38
  • This explains things in great detail and thoroughly answers the question: https://stackoverflow.com/a/5034547/1854328 – Robert Mauro Feb 01 '19 at 15:38

2 Answers2

84

The square brackets produce a list/array.

The curly brackets produce an object with key/value pairs.

The list can then be a value of a key/value pair.

jvyden
  • 89
  • 1
  • 7
Boy
  • 7,010
  • 4
  • 54
  • 68
  • doesn't the "product" depends on the implementation of deserializing function in each language ? Isn't json just a few rules for plain text ? I thought only programming languages can have rules for "producing datatypes or objects" – illevens Oct 11 '21 at 15:05
64

[] means an array of object (a list) and {} means it will be an object.

Example:

{
    "ID":"test",
    "sports": [
        "volley-ball",
        "badminton"
    ]
}

To get the ID, you can do: myjsonobject.ID (here you will get "test")

And for sports: myjsonobject.sports[0] (here you will get "volley-ball")

Community
  • 1
  • 1
Samuel LEMAITRE
  • 1,041
  • 7
  • 8