Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is "HelloWorld" a valid json response

Tags:

json

rest

wcf

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)?

like image 734
NullReference Avatar asked Mar 15 '12 20:03

NullReference


People also ask

What is considered valid JSON?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON. Unlike in JavaScript code in which object properties may be unquoted, in JSON only quoted strings may be used as properties.

Is a valid JSON value?

Yes, according to ECMA-404 The JSON Data Interchange Standard. A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar. And following, the JSON value grammar is given as: A JSON value can be an object, array, number, string, true, false, or null.

Is a string a valid JSON response?

Yes, in most contexts. It is valid JSON syntax representing a JSON value.

What are JSON responses?

json() The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .


2 Answers

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.

like image 118
arvidkahl Avatar answered Sep 29 '22 04:09

arvidkahl


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.

like image 32
Daniel Pittman Avatar answered Sep 29 '22 04:09

Daniel Pittman