Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a plain string valid JSON? [duplicate]

Tags:

json

Is a plain string valid JSON or does there have to be an object?

For example:

"morpheus"

versus:

{
    "name": "morpheus"
}
like image 571
Sam Berry Avatar asked Jun 12 '14 12:06

Sam Berry


3 Answers

It is valid in Javascript.

You might get confused at first trying to test this:

JSON.parse("bob");

This would fail with the error: "Unexpected token b". However, that's the equivalent of passing just plain bob as the text in the response, not "bob". If you add the quotes:

JSON.parse('"bob"')

You get the simple string "bob" back as you should.

like image 195
user3413723 Avatar answered Oct 28 '22 00:10

user3413723


Important

This answer once said No, the first character of the JSON must be a { or a [.

At the time I wrote that, I was testing it with Python. In Python (2.7.x at least), json.loads("a") gives an error, which means that a plain string is not valid JSON there.

It has been rightfully pointed out by others that it cannot be said that a plain string is not valid JSON. See this question for a more appropriate answer.

At this time all I can say is that it depends on your environment. In javascript it may be valid, in python it may not be, etc, etc.

JSON stands for JavaScript Object Notation

Here is a quote from the official site

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

In JSON, they take on these forms:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

Take note of the text I bolded.

Because of that, and JSON being JS Object Notation, it is implied that a JSON representation of a name:value pair must always be in the form of

{
  "name": "value"
}

Note that you can also make the 'root object' a list

[
  {
    "name1": "value1"
  },
  {
    "name2": "value2"
  }
]

This basically means that the JSON contains more than one object.


As Sunny R Gupta pointed out, this is also valid JSON

[
  "this",
  "is",
  "valid"
]

Note that this works because the strings are not in the form "name":"value" but just strings. Taking that into consideration valid options for your example would be

{
  "name": "Morpheus"
}

or

[
  "morpheus"
]

The first character of the JSON must be a { or a [

like image 23
Tim Avatar answered Oct 28 '22 00:10

Tim


UPDATE: 2018:

It has been 4 years since I originally answered this question. Back then plain strings in quotes were not valid JSON. As of today, it is being accepted as a valid JSON.

The following is kept for people to see what the error used to be earlier:


Parsing a simple string gives:

Parse error on line 1:
"morpheus"
^
Expecting '{', '['

indicating that it needs to be an object or an array.

TIP: To validate JSON strings and see what works and what does not, try using http://jsonlint.com

like image 27
Sunny R Gupta Avatar answered Oct 28 '22 00:10

Sunny R Gupta