Sunday, September 29, 2013

Cold Sunday morning code

It's been interesting morning. I woke up way to early on account of the cold. A cup of coffee later, I decided to sign up and try out these free NoSQL backends which could be used for AngularJS apps. This is where I stumbled upon Mongolab.com. Mongolab is a mongodb as a service website and gives out a free 500MB account which is enough when you're trying out their stuff. But trust me when I tell you that unless your pulling 5,000 hits per month on your app that 500MB will last for a long bit.

Since I was on a roll getting free shit on the Internet, I decided to also get a github.com account. You can only ride a joke for so long, right Josan Astrid Dometita?

Amazingly, while I was trying to learn how to use the MongoLab APIs, I also found out that someone has already written an adapter for it for AngularJS.

It didn't take long to get all the stuff together. Twitter-bootstrap for my interface framework, AngularJS as my app framework, Mongolab for my backend. And since the point was to learn how to do stuff, I decided to do a simple contacts app with full CRUD features. It was easy as pie!

Here's the github repo for the code if you want to study it also: https://github.com/killertilapia/Mongolabular

Now with that done, we'll see what other stuff I can do with it. *knowing grin*

Also, I can now tag Google+ friends in my blog! SWEET! +Romar Mayer Micabalo +Paul Michael Labis +Jon Doblados +Arthur Vincent Simon +Lionel Amarado

Sunday, September 8, 2013

Parsing JSON with keys that have the at sign or @ symbol

I had to work with this interesting JSON file which had a metadata section which can be accessed with @metadata key. Here's a small sample of the JSON file:

 "twitter_handle": "..........",
 "website": ".............",
 "@metadata": {
  "Access-Control-Allow-Credentials": "true",
  "Raven-Entity-Name": "4fff7413",
  "@id": "4fff7413-f1bd-4e00-ab51-6754f1111c03",
  "Last-Modified": "2013-09-08T08:29:35.2587326Z",
  "Raven-Last-Modified": "2013-09-08T08:29:35.2587326",
  "@etag": "01000000-0000-0003-0000-000000000001",
  "Non-Authoritative-Information": false
 }

The twitter_handle or the website data are just normal JSON values so they are not that interesting but the @metadata is where it gets interesting. How do you access that?

At first I tried object.@metadata, didn't work. So I tried the next one which is object[@metadata] which also didn't work. I then found out over at StackOverflow that I was quite near. All I had to do was put single quotes so it would look like object['@metadata'].

It also works if you're trying to get a value inside, like say @id within @metadata. It would end up looking like object['@metadata']['@id'].