5

This could be the most basic json question ever. I'm creating a WCF REST service and have a HelloWorld test function that just returns a string. I'm testing the service in fiddler and the reponse body I'm getting back is:

"HelloWorld"

I also created a function that would just return a number (double) and the response body is:

1.0

Are those valid json responses? Are simple return types just returned in plain text like that (no markup with the brackets)?

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
NullReference
  • 1,205
  • 2
  • 9
  • 7
  • See also http://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json, where the question was phrased more broadly. – Johann Mar 19 '14 at 04:17

4 Answers4

6

Valid JSON responses start with either a { for an object or [ for a list of objects.

Native types are not valid JSON if not encapsulated. Try JSONlint to check validity.

arvidkahl
  • 852
  • 8
  • 15
5

RFC 4672, says no. Which doesn't mean it can't work, but it isn't strictly standards compliant. (Of course, neither are all the JSON readers...)

To quote from Section 2, "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

Only objects / maps, and arrays, at the top level.

Community
  • 1
  • 1
Daniel Pittman
  • 16,733
  • 4
  • 41
  • 34
2

According to the official website, you need to use a syntax like this:

JSON Syntax

You need to declare what you want beetween {}, like this:

{
    "test": "HelloWorld"
}
Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
  • Not quite right, since an array is valid JSON too. Unfortunately json.org isn't explicit on what are valid root nodes in the grammar. – John Carter Mar 15 '12 at 20:58
0

No. The following is, for example:

{
    "Foo": "HelloWorld"
}

You can try JSONLint to see what validates and what does not.

Tommi
  • 8,550
  • 5
  • 32
  • 51