15

Most place the JSON is in format like

{
    color: "red",
    value: "#f00"
}

Or

[  
    { color: "red",     value: "#f00"   },
    { color: "red",     value: "#f00"   }
]

And I want to ask is primitive type like string, bool, int also JSON?

I have found follow link,

http://json-schema.org/latest/json-schema-core.html

http://www.json.org/

https://zh.wikipedia.org/wiki/JSON

https://www.ietf.org/rfc/rfc4627.txt

http://www.c-sharpcorner.com/uploadfile/3d39b4/data-types-in-json/

And,

In RFC4627 it says

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

A string is a sequence of zero or more Unicode characters [UNICODE].

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

An array is an ordered sequence of zero or more values.

The terms "object" and "array" come from the conventions of JavaScript.

So I reading it as pure string, number boolean like

"a string"

100

true

Are all JSON,

But two of my colleagues think that primitive types can only be value in object,

{ ASTRING : "astring" } is JSON,

And if there's only "a string", this is not called, as it is not JSON format,

I think I, and my colleagues may not be professional enough to judge it,

So I want to know, is pure primitive type JSON?

.

Another idea for me is that , JSON is called a convenient way to exchanging data, but if this format didn't support pure string,

that is, if I just want to throw out a string, I can't use JSON to do it?

and have to force it change to { Message : "a message"},

and cannot use a way which I think is simpler just throw "a message" ...?

yu yang Jian
  • 6,680
  • 7
  • 55
  • 80

3 Answers3

10

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
Community
  • 1
  • 1
Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
3

{ "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.

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
  • But is "astring" -- as yu yang Jian seems to me to be asking? If the quote from RFC4627 above is correct and complete, it seems to me "yes". –  Jan 19 '17 at 02:37
  • @Roadowl: according to RFC4627(2, JSON Grammar), "A JSON text is a serialized object or array." The OP's quote above is lifted from the introduction, which provides a cursory glance at what you can do with JSON, not its formal properties. – David Titarenco Jan 19 '17 at 02:44
  • 1
    You only provided the definition for "an object", whereas OP seems to want to know if numbers, strings, booleans are valid under the JSON spec, and that is "yes", they are JSON *values*. Do `JSON.parse(2)` is Javascript, returns 2. Same for strings and true/false – OneCricketeer Jan 19 '17 at 02:52
  • @cricket_007: I provided the definition for a JSON *text*. Per the standard, as mentioned above, `"test"` is NOT valid JSON text, no matter what any specific parser says. JSON parsers are notoriously terrible, anyway. – David Titarenco Jan 19 '17 at 03:12
  • 3
    Just a small update. There's actually another JSON spec (https://tools.ietf.org/html/rfc7159) which DOES allow pure literals in JSON text. So, yay, confusion. – David Titarenco Jan 20 '17 at 01:04
  • 1
    @DavidTitarenco: It's not just *another JSON spec*. It's a standards track RFC that explicitly obsoletes the informational RFC 4627. – Mike Sherrill 'Cat Recall' Jan 21 '17 at 23:21
  • @MikeSherrill'CatRecall' -- true, except that it's terrible compared to RFC4627, and in some cases even straight-up wrong (JSON is *not* a subset of JavaScript, for example). Therefore, some parsers still use 4627. I just recently found out about some of its blunders by reading this excellent article: http://seriot.ch/parsing_json.php -- as an aside, OP cites 4627 and not 7159. – David Titarenco Jan 22 '17 at 04:29
  • @DavidTitarenco: You can submit corrections for errors. See [Errata](https://www.rfc-editor.org/errata_search.php?rfc=7159). What a parser does isn't relevant to answering the OP's question. – Mike Sherrill 'Cat Recall' Jan 22 '17 at 06:54
0

FWIW, here's output from the command line JSON parser prgram jq:

$ echo "{ foo }" | jq .
parse error: Invalid literal at line 1, column 6

$ echo "{ \"foo\" }" | jq .
parse error: Objects must consist of key:value pairs at line 1, column 9

$ echo "\"foo\"" | jq .
"foo"

I don't have access to other parser at the moment.

  • This isn't worth much. It's pretty common knowledge that many/most JSON parsers happily parse standalone primitives, but that doesn't guarantee anything about JSON or its spec in general, which is what the question is asking. – ggorlen Oct 31 '22 at 00:31