Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct format in JSON, should I quote names also?

Tags:

json

I am writing a JSON file, but I am not sure about which of the following formats is the correct one?

Quoting variable names and all string values

{
    "class": {
        "number": 2,
        "student": {
            "name": "Tom",
            "age": 1
        },
        "student": {
            "name": "May",
            "age": 2
        }
    }
}

or

Quoting only string values

{
    class: {
        number: 2,
        student: {
            name: "Tom",
            age: 1
        },
        student: 
        {
            name: "May",
            age: 2
        }
    }
}  
like image 605
Questions Avatar asked Oct 06 '10 01:10

Questions


People also ask

Do JSON field names need quotes?

In JSON each property name and each string value must be enclosed in double quotation marks ( " ). In JavaScript notation, a property name used in an object literal can be, but need not be, enclosed in double quotation marks. It can also be enclosed in single quotation marks ( ' ).

What is the proper format of JSON?

JSON Syntax RulesData is in name/value pairs. Data is separated by commas. Curly braces hold objects. Square brackets hold arrays.

Should JSON have quotes?

JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format and fully described on www.json.org. JSON is based on JavaScript but the format is stricter. JSON requires double quotes around keys whereas JavaScript does not.


1 Answers

The first is valid, if you're unaware you can validate your JSON output online pretty easily here: http://www.jsonlint.com/

like image 64
Nick Craver Avatar answered Nov 15 '22 19:11

Nick Craver