0

I'm going to use JSON to stringify the value of a property of an object in order to store it as a Tag on Google Calendar through Google Apps Script. The value is actually a double-nested Object (see extraAttributes in myObject).

I have an Object that is built like the following:

var myObject = {
  "location": "somwehere",
  "date": new Date(),
  "numSomething": 20,
  "extraAttributes": {"used": true, "interval": 60, "internals": {"timer": 10, "visible": false} }
}

I tried to make it pretty and readable... anyway: myObject actually has anywhere from 20-40 properties, some of which are nested Objects. So here's the question:

Is there a way to notify if the value of a property in an Object is an object itself (would extra levels of nesting be an issue for this detection?)? The reasoning behind this is that I don't know how JSON.stringify and JSON.parse will affect the other data (I've tested it on that one particular value of type Object and it worked fine), and also that I don't know how much of a performance impact that those two functions would have on my script if the properties being stored reach 20-40.

I would rather do a check to see if the value is an Object and stringify only that (would that also be inefficient?). Feel free to lecture me on Objects and nesting if this would cause major problems in the future ;)

8
  • I'm a little confused. Are you saying that in order to reduce the number of key/value pairs in the resulting JSON, you'd like to avoid anything at the top level that isn't an object? Commented Jul 12, 2013 at 15:49
  • Hmm... I'm confused by you also :P I meant to say that I want to check to see if it is an Object before setting the Tag, because the only thing that actually needs to be JSON.stringify-ed are the values that are Objects. So anything that is a String, Integer, Date, etc. can just be tagged normally. The Object needs to be stringified, but I didn't want to do "screw it, stringify everything", I wanted to only stringify the values that needed it to be tagged correctly. Does that make sense? In the end, I completely forgot about typeof, and will be using that to accomplish the goal :) Commented Jul 12, 2013 at 15:54
  • To be honest, it doesn't make a lot of sense to me. It now sounds like you want an object that has some properties (the ones that have a nested object) serialized to JSON, and you want to keep the rest as native JavaScript. I'm wondering what the point would be. Commented Jul 12, 2013 at 15:58
  • Crud, sorry. I tried to explain it as best I could. I'll try again! Basically, I'm storing all the property/value pairs of an Object (myObject above) into Tags on Google Calendar Events. The Tags can be pretty much anything except Objects, functions, etc. 90% of the data in myObject can just be tagged normally, but if I want to retain the data that is stored in a nested object (extraAttributes above), then I need to stringify it. I wanted to avoid stringifying everything, so I wanted to check to see if the typeof extraAttributes === 'object', so it can be serialized and tagged. Commented Jul 12, 2013 at 16:08
  • Oh, that's strange. It doesn't let you store nested objects? If you're testing all properties, be careful, because typeof null === object; // true Commented Jul 12, 2013 at 16:10

2 Answers 2

1

All Javascript values, except functions and undefined, can be serialized as JSON.

To answer your question, see the typeof operator.

Sign up to request clarification or add additional context in comments.

1 Comment

Well dang, I've seen typeof in various JavaScript questions on Stack Overflow... but that didn't even cross my mind and I couldn't tell you why! Well, hopefully someone else runs across this question in the future (wasn't on SO particularly before now), so I don't feel so silly for asking it :P
1

Another way to check if something is an object instead of using typeof (which can be misleading in some cases — for example typeof [] === 'object') is to use the Object constructor:

var foo = { id: 1 };
var bar = 'string';
console.log(foo === Object(foo)) // true
console.log(bar === Object(bar)) // false

2 Comments

I didn't know about the Object constructor, very useful! Would you say that using the constructor is preferred over typeof? I find the Object constructor to be more readable, but I wonder about the performance costs. Not like I have any system that really needs millisecond boosts, but it would be useful to know the difference in any case!
@ChrisCirefice, I actually use underscore.js (underscorejs.org) for pretty much everything and I typically use its type detection (for example _.isArray, _.isObject) for consistency. Internally, underscore uses the Object constructor for testing objects, which is where I learned this. The annotated source for underscore is super educational and I highly recommend reading through it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.