4

I have a method that receives something and it needs to determine the type of the received value. I can use the typeof thing to perform regular comparisons like if it is a number or a string. But how can I do this for JSON objects? Comparing them with JSON brings up the error:

Uncaught TypeError: Expecting a function in instanceof check, but got #< Object>

So I guess that comparing a JSON object type with JSON is not the way?

The original code is like:

check = (what) ->
  if what instanceof JSON
    alert "Yooo"

check({compare: "me"})
noncom
  • 4,962
  • 3
  • 42
  • 70
  • 2
    Anyway the question doesn't make sense; JSON is either a string or an object; there's no JSON "type" in JavaScript. – Pointy Jul 01 '12 at 19:11
  • @Pointy yes, excuse me, it was `instanceof` :) fixed that. – noncom Jul 01 '12 at 19:13
  • There is no such thing as a JSON Object unless you mean the native `window.JSON` object which hosts 2 methods. – Esailija Jul 01 '12 at 19:31

4 Answers4

4

The type will be object, not JSON. To see what you're working with, you can check if it has the properties you're looking for. Check the length, or if it has specific keys.

Here's a pretty good informational page on working with JSON. JSON in JavaScript

sachleen
  • 30,730
  • 8
  • 78
  • 73
1

First type of return string, second, there is no such thing as Type JSON in type of possible return values. see the this page for detail

in your case you will receive "object".

LAamanni
  • 177
  • 2
  • 13
Raab
  • 34,778
  • 4
  • 50
  • 65
1

JSON stands for JavaScript Object notation and is simply a name for how object literals are written in JavaScript it is not a type.

var a = {"foo":"My foo","bar" : 4};
var b = {"foo":"My foo","bar" : 0};
var c = {"foo":"My c foo","barella" : -1};
var d = '{"baz":"My baz","bar" : 4}';

a,b,c and d are all objects the first three of type object the fourth of type string. You could from a type theoretic point of view say that the first two have the same type. If you did eval("var e =" + d) then the string would be in d would be evaluated and since d is an object serialized to JSON the result would be a valid object that would be assigned to e.

in other words JSON is no type it's part of the JavaScript grammar, the result of evaluating JSON is an object and the type of that object will vary depending on the object literal. using typeof on such an object will yield "object" (regards less of the type theoretic type of the object).

If you wish to test and object against a specific type you would therefor have to test it for all the expected properties and methods

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • 1
    In your example a, b, and c are all objects and d is a string, **but _none_ are JSON**. d is _almost_ JSON except that it doesn't have double-quoted property names which are required by [JSON](http://json.org) grammar. JSON is _not_ part of JavaScript grammar. The result of _parsing_ JSON is an object. The term "JSON" applies only to string representations of objects as defined at [json.org](http://json.org). Note also that not all JavaScript objects can be represented as JSON (because JSON doesn't have an `undefined` type and doesn't do functions). – nnnnnn Jul 01 '12 at 21:36
  • @nnnnnn JavaScript object notation === JSON. Quotes are optional and all object literals in JavaScript are written using JavaScript object notation. The Hole point is that JSON is valid JavaScript and my point with the objects above are to show why JavaScript object notation is not a type – Rune FS Jul 02 '12 at 05:26
  • Yes, "JSON" is an acronymn for JavaScript Object Notation, but JSON is a _string_ format for _data interchange_ and its grammar is a subset of what is valid in JavaScript. (This means JSON can be used with just about any modern programming language - that's the point.) JS object literals in JS code are _not_ JSON. Quotes are optional in JS object literals but not in JSON - missing quotes doesn't matter if you're going to use `eval` on your JSON, but that's bad practice. More info [here](http://stackoverflow.com/questions/6489783/whats-the-difference-between-javascript-object-and-json-object). – nnnnnn Jul 02 '12 at 06:16
  • @nnnnnn I'm with you on the quotes part (which was new to me since all the parsers I've worked with really don't care) the rest however is not accurate according to rfc4627. What you define as JSON is a JSON **text** and not the format it self. And to be precise it's a subset of (derived from) the ECMAScript grammar. And all this is still irrelevant to the answer and question since the examples are about JSON not being a type but a format – Rune FS Jul 02 '12 at 06:55
  • When the actual acronym "JSON" is used it refers to the data interchange format that was based on JS object literal syntax, _not_ to actual JS code. These are two distinct concepts. If you read rfc4627 it says that "JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is derived from the object literals of JavaScript" - that is, it backs up what I'm saying not what you're saying. Note that http://json.org presents the same info as rfc4627 in a more readable format. – nnnnnn Jul 02 '12 at 07:02
  • @nnnnnn to be even more precise d is **not** JSON because the definition **requires** the first character to be {. JSON is often represented with string literals but those are platform dependant and might or might not require quotes or other leading characters. – Rune FS Jul 02 '12 at 07:04
  • Fair enough about other platforms, but in this case the first character is a { because it is the content of the string that matters, not how it is created. Note that valid JSON can start with a square bracket [. – nnnnnn Jul 02 '12 at 07:13
1
class JSON
   constructor: (@data) ->

   get: (key) ->
     @data[key]

   set: (key, value) ->
     @data[key] = value

a = new JSON "foo":"My foo", "bar" : 4

a.get('foo')
a.data.foo
a.data['foo']

console.log(a instanceof JSON)

:D You really shouldn't be doing this though, at least not to create a JSON type. But it's possible that you can create your own wrapper for pretty much anything. Combining this with the Object.defineProperty to setup getters and setters based on @data, you could do some powerful stuff. It doesn't have method_missing methods, but you can achieve similar results with Object.defineProperty

SMathew
  • 3,993
  • 1
  • 18
  • 10