Storing json (facebook api) in database & retrieve them

468 views Asked by At

I've been storing some data from facebook api using koala gem in database :

fb_data = graph.get_connections(...)
fb_data.each do |t|
    obj.raw_data = t
    obj.save
end

and it looks like that when I extract it from database :

--- id: '111222333444' from: name: James B id: '44444333332222' story: XXX ...

It's not a format I familiar with, thus I was wondering if I did something wrong ?

2

There are 2 answers

2
titibouboul On BEST ANSWER

So here is the answer, I found with the help of @miguelgraz :

fb_data = graph.get_connections(...)
fb_data.each do |t|
    obj.raw_data = YAML::dump(t) #Serialize the hash
    obj.save
end

and then, to retrieve data :

t = YAML::load(t) #deserialize it
3
Miguelgraz On

You're probably saving it in its raw format indeed, try using something like t.to_json to save a json of this info, maybe something like

fb_data = graph.get_connections(...)
fb_data.each do |t|
  obj.raw_data = t.to_json
  obj.save
end