Encoding Javascript Object to Json string

Unless the variable k is defined, that’s probably what’s causing your trouble. Something like this will do what you want: var new_tweets = { }; new_tweets.k = { }; new_tweets.k.tweet_id = 98745521; new_tweets.k.user_id = 54875; new_tweets.k.data = { }; new_tweets.k.data.in_reply_to_screen_name=”other_user”; new_tweets.k.data.text=”tweet text”; // Will create the JSON string you’re looking for. var json = JSON.stringify(new_tweets); … Read more

JavaScript, elegant way to check nested object properties for null/undefined [duplicate]

You can use an utility function like this: get = function(obj, key) { return key.split(“.”).reduce(function(o, x) { return (typeof o == “undefined” || o === null) ? o : o[x]; }, obj); } Usage: get(user, ‘loc.lat’) // 50 get(user, ‘loc.foo.bar’) // undefined Or, to check only if a property exists, without getting its value: has … Read more

Using reserved words as property names, revisited

In ECMAScript, starting from ES5, reserved words may be used as object property names “in the buff”. This means that they don’t need to be “clothed” in quotes when defining object literals, and they can be dereferenced (for accessing, assigning, and deleting) on objects without having to use square bracket indexing notation. That said, reserved … Read more