Perform a `typeof JSON` comparison

coffeescript, javascript, json

Solution

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

Problem

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"}) ```

Original source

Related problems