Is a primitive type considered JSON?

{ "astring" } is not valid JSON and neither is "astring" or astring, as we need both a key and a value, e.g. { KEY : VALUE } where KEY is always a string and VALUE can be a string, number, boolean, or null.

From the spec:

JSON Spec

Yes, as the spec says, JSON can be a top level primitive value without an object wrapping it. – Andy Ray

If I understood it correctly, that comment is not correct. Valid JSON is never a top-level primitive value by itself. If you're still confused, this should clear things up:

  1. JSON Grammar

    A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

    A JSON text is a serialized object or array.

    JSON-text = object / array

    These are the six structural characters:

    begin-array = ws %x5B ws ; [ left square bracket

    begin-object = ws %x7B ws ; { left curly bracket

    end-array = ws %x5D ws ; ] right square bracket

    end-object = ws %x7D ws ; } right curly bracket

    name-separator = ws %x3A ws ; : colon

    value-separator = ws %x2C ws ; , comma

    Insignificant whitespace is allowed before or after any of the six structural characters.


The relevant RFC is RFC 7159, not RFC 4627. RFC 4627 is "informational". RFC 7159 is "standards track"; it explicitly obsoletes RFC 4627.

Request for Comments: 7159                                  Google, Inc.
Obsoletes: 4627, 7158                                         March 2014
Category: Standards Track
ISSN: 2070-1721

In the text of RFC 7159, you'll find this.

13.  Examples

   This is a JSON object:

      {
        "Image": {
            "Width":  800,
            "Height": 600,
            "Title":  "View from 15th Floor",
            "Thumbnail": {
                "Url":    "http://www.example.com/image/481989943",
                "Height": 125,
                "Width":  100
            },
            "Animated" : false,
            "IDs": [116, 943, 234, 38793]
          }
      }
   [snip]
   Here are three small JSON texts containing only values:

   "Hello world!"

   42

   true

Tags:

Json