Exercises: JSON

  1. Which of the following three code snippets is correct JSON syntax? Why are the other two options incorrect?

    a.

    1
    2
    3
    4
    5
    
    {
       type: "dog",
       name: "Bernie",
       age: 3
    }

    b.

    1
    2
    3
    4
    5
    
    {
       "type": "dog",
       "name": "Bernie",
       "age": 3
    }

    c.

    1
    2
    3
    4
    5
    
    {
       "type": 'dog',
       "name": 'Bernie',
       "age": 3
    }

    a. This not correct JSON syntax because keys in JSON need to be strings. b. This is the only correct option. c. This not correct JSON syntax because, unlike JavaScript, JSON only uses double quotation marks ("")

  2. Which of the following three code snippets is correct JSON? Why are the other two options incorrect?

    a.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    {
       "animals": [
          {
                "type": "dog",
                "name": "Bernie",
                "age": 3
          },
          {
                "type": "cat",
                "name": "Draco",
                "age": 2
          }
       ]
    }

    b.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    {
       [
          {
                "type": "dog",
                "name": "Bernie",
                "age": 3
          },
          {
                "type": "cat",
                "name": "Draco",
                "age": 2
          } 
       ]
    }

    c.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    [
       {
             "type": "dog",
             "name": "Bernie",
             "age": 3
       },
       {
             "type": "cat",
             "name": "Draco",
             "age": 2
       } 
    ]