Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active December 24, 2015 22:39
Show Gist options
  • Save dannguyen/6874608 to your computer and use it in GitHub Desktop.
Save dannguyen/6874608 to your computer and use it in GitHub Desktop.
Convert facebook feed to flat array and discard large fields
require 'json'
feed_owner_id = feed_array.first['id'].split('_')[0]
vals = feed_array.map do |post|
next if post.nil? || post.empty?
hsh = {}
if !post['from']
hsh[:from] = 'N/A'
elsif post['from']['id'] != feed_owner_id
hsh[:from] = "#{post['from']['id']} (Fan)"
else
hsh[:from] = feed_owner_id
end
post.reject{|k, v| v.is_a?(Hash) || v.is_a?(Array) }.each_pair do |k,v|
if v.to_s.length < 100
hsh[k] = v
else
hsh[k] = v.to_s[0..100] + "..."
end
end
hsh
end.compact
fname = "#{feed_owner_id}-flat-fb-feed.json"
File.write(fname, JSON.pretty_generate(vals))
`in2csv #{fname} > #{feed_owner_id}-flat-fb-feed.csv`
full_json_fname = "#{feed_owner_id}-nested-fb-feed.json"
File.write(full_json_fname, JSON.pretty_generate(feed_array))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment