$http GET boolean

1,746 views
Skip to first unread message

Mark Volkmann

unread,
Jan 7, 2014, 6:07:11 PM1/7/14
to ang...@googlegroups.com
I'm invoking a REST service that returns a boolean as JSON.
The response Content-Type header is "application/json".
The raw text of the response is:
false
Note there are no quotes around it.
When I get the value in the success method, it is the string "false".
Shouldn't I get the boolean value false?

--
R. Mark Volkmann
Object Computing, Inc.

Daniel Tabuenca

unread,
Jan 7, 2014, 11:39:40 PM1/7/14
to ang...@googlegroups.com
A raw text of just false is not valid JSON and as such is probably not parsed by the browser as JSON.

Values in JSON have to be wrapped in either an array or an object, you can't just have a constant value sitting as the root like that.

Sander Elias

unread,
Jan 7, 2014, 11:46:37 PM1/7/14
to ang...@googlegroups.com
Hi Mark,

So the raw text you get is a string. This is not that surprising, once you have written a JSON parser :)
to be short, most JSON parsers expect an [ or an { as first token, and if not found, most of the times, they just give back what is found.
While your response is technically correct JSON, most parsers don't handle single value responses.

Regards
Sander

Daniel Tabuenca

unread,
Jan 8, 2014, 12:18:17 AM1/8/14
to ang...@googlegroups.com
But it's NOT technically correct JSON. The JSON RFC clearly states that the top-level item is either an object or an array, not a value.

Sander Elias

unread,
Jan 8, 2014, 12:47:58 AM1/8/14
to ang...@googlegroups.com
Hi Daniel,

Can you point me to that section? I could not find it on JSON.org neighter in the ecma 404 spec. (that name still cracks me up ;) ) I just read the json rfc (wasn't aware of that one!) and it is indeed stated in there. 
Still, the rfc isn't the formal standard, the ecma 404 is, and in there it's not! So, formally speaking, it is technically correct. 
however I do think it's a bad idea to use it like that! and most parsers don't parse it correctly!

Regards
Sander




Daniel Tabuenca

unread,
Jan 8, 2014, 1:05:04 AM1/8/14
to ang...@googlegroups.com
I think you're right, the ecma 404 spec changed from prior versions of RFCs, although I've run into many parser implementations that go off the older specifications that had the "A JSON text is a serialized object or array." bit in them.

Sander Elias

unread,
Jan 8, 2014, 1:14:02 AM1/8/14
to ang...@googlegroups.com
Daniel,

It was never on the original spec (JSON.org). I wasn't aware of the rfc. I did write an JSON parser, and during that, I found out that Crockfords own reference parser didn't handle the single value either. I think that's where the confusion came from.

Regards
Sander

Daniel Tabuenca

unread,
Jan 8, 2014, 1:17:03 AM1/8/14
to ang...@googlegroups.com

Sander Elias

unread,
Jan 8, 2014, 1:26:13 AM1/8/14
to ang...@googlegroups.com
Daniel,

I know, I had just read the entire thing. it is indeed in there as I said in a previous message ;)

Regards
Sander

Mark Volkmann

unread,
Jan 8, 2014, 6:33:37 AM1/8/14
to ang...@googlegroups.com
I think it's interesting that browsers, at least Chrome, handle this the way I expected.

If you run this in the dev tools console, you'll get boolean:
typeof JSON.parse('false')

If you run this, you'll get string:
typeof JSON.parse('"false"')

Does Angular use it's own JSON parser instead of the JSON.parse?


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Daniel Tabuenca

unread,
Jan 8, 2014, 10:27:25 AM1/8/14
to ang...@googlegroups.com
It actually does use JSON.parse, but first it checks if the JSON starts/ends with:

```javascript
 var JSON_START = /^\s*(\[|\{[^\{])/,
      JSON_END = /[\}\]]\s*$/,
```

If it doesn't match those it is not parsed as JSON at all and just returned as a string.


Mark Volkmann

unread,
Jan 8, 2014, 11:11:32 AM1/8/14
to ang...@googlegroups.com
Hmm ... perhaps it should not do that. I think it should support parsing of string, number and number primitive values. It seems that simply removing that check would make it work.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Gerardo Lima

unread,
Nov 29, 2017, 5:37:24 AM11/29/17
to Angular and AngularJS discussion

For the current version of both Chrome and Firefox, it doesn't happen anymore:
typeof JSON.parse('false') == "boolean"
typeof JSON.parse("false") == "boolean"
Reply all
Reply to author
Forward
0 new messages