12

A valid JSON Syntax is something of the kind:

{
  "username": "admin",
  "password": "123"
}

But what if I want to transmit an array of 'users' (given the example), instead of a single 'user' ?

Is the code below Valid JSON, according to the specifications?

[{
  "username": "admin",
  "password": "123"
}, {
  "username": "bbvb",
  "password": "sdfsdf"
}, {
  "username": "asd",
  "password": "222"
}]

And if not, what is the best way to transmit an array of values across with JSON? (And with 'best way', I mean syntactically)

MikO
  • 18,243
  • 12
  • 77
  • 109
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360

4 Answers4

11

Yes, your example is valid JSON - that is exactly how you want to use an array.

Edit : Here is a good link on JSON and its usage.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
5

The not-very-well-known page json.org has a diagram that shows the syntax. It’s extremely simple to understand, IMHO.

Bombe
  • 81,643
  • 20
  • 123
  • 127
2

Json Syntax Includes following.

 1. Data is represented in name/value pairs.
 2. Each name is followed by ':'(colon).
 3. The name/value pairs are separated by ,(comma).
 4. Json object starts and ends with '{' and '}'.
 5. Square brackets '[ ]' hold arrays and values are separated by
    ,(comma).

Json Objects Example

    {
    "id":"21",
    "language": "Json",
    "edition": "second",
    }

Json Array Example

  {
        "book": [
        {
        "id":"21",
        "language": "Json",
        "edition": "second"
        },
        {
        "id":"42",
        "language": "Json",
        "edition": "third"
        }]
        }

I have taken reference from http://www.tutsway.com/json-syntax.php

Karel Kral
  • 5,297
  • 6
  • 40
  • 50
Manish
  • 328
  • 3
  • 9
1

What you wrote up there is already correct :)

[{ "username" : "admin", "password" : "123" }, { "username" : "bbvb", "password" : "sdfsdf" }, { "username" : "asd", "password" : "222" }]
sikachu
  • 1,212
  • 8
  • 16