Skip to content

Instantly share code, notes, and snippets.

@bretcj7
Created October 11, 2018 01:05
Show Gist options
  • Save bretcj7/c59b041b95aa8dab13fa983f672d619e to your computer and use it in GitHub Desktop.
Save bretcj7/c59b041b95aa8dab13fa983f672d619e to your computer and use it in GitHub Desktop.
slice hash
Only extract: Name, Owner's login, html_url and score
The nested part of Owner got me.
{code}
keys_to_extract = ['name', 'html_url', 'stargazers_count']
json_trimmed = json['items'].map do |v|
v.select { |k, _| keys_to_extract.include? k }
#v.dig('owner', 'login')
end
example hash:
{
"total_count": 1546451,
"incomplete_results": false,
"items": [
{
"id": 8514,
"node_id": "MDEwOlJlcG9zaXRvcnk4NTE0",
"name": "rails",
"owner": {
"login": "rails",
"id": 4223
},
"html_url": "https://github.com/rails/rails",
"description": "Ruby on Rails",
"fork": false,
"score": 1.0
},
{
"id": 8514,
"node_id": "MDEwOlJlcG9zaXRvcnk4NTE0",
"name": "rails",
"owner": {
"login": "rails",
"id": 4223
},
"html_url": "https://github.com/rails/rails",
"description": "Ruby on Rails",
"fork": false,
"score": 1.0
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment