Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the purpose of using square brackets in json? [duplicate]

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

like image 524
Uswer721 Avatar asked Apr 18 '16 07:04

Uswer721


People also ask

Why square brackets are used in JSON?

Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma).

What are square brackets used for in coding?

What Does Bracket Mean? Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.

What are square brackets used for in JavaScript?

The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.

Why we use square brackets for array?

They are used with many different things including classes, functions, loops, and conditionals. Square brackets are used to index (access) elements in arrays and also Strings. Specifically lost[i] will evaluate to the ith item in the array named lost.


2 Answers

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.

like image 94
Boy Avatar answered Sep 23 '22 07:09

Boy


[] 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")

like image 33
Samuel LEMAITRE Avatar answered Sep 20 '22 07:09

Samuel LEMAITRE