Merging and manipulating JSON arrays in ruby -
in model i'm getting 2 json arrays both facebook's , twitter's api.
facebook_friends = { "data": [{"name": "friend joe", "id": "123"}, {"name": "friend jane", "id": "12342"}]} twitter_friends = { "users": [{"name": "other friend joe", "id": "333"}, {"name": "other friend jane", "id": "456"}]}
and want build array (nb: i'm appending provider key identify source of data)
all_friends = [ {"name": "friend joe", "id": "123", "provider": "facebook"}, {"name": "friend jane", "id": "12342", "provider": "facebook"}, {"name": "other friend joe", "id": "333", "provider": "twitter"}, {"name": "other friend jane", "id": "456", "provider": "twitter"}]
i can jquery -> http://jsfiddle.net/gm3jj/ how do in ruby? thanks
if want combine stuff, can this:
# array of friends each service , add together provider fb = json.parse(facebook_friends)["data"].map {|x| x["provider"] = "facebook"} tw = json.parse(twitter_friends)["users"].map {|x| x["provider"] = "twitter"} # concatenate them 1 array fb + tw
ruby-on-rails ruby ruby-on-rails-4
No comments:
Post a Comment