Skip to content

Instantly share code, notes, and snippets.

@ramhoj
Created February 13, 2017 10:56
Show Gist options
  • Save ramhoj/6dd2c8c9447ff029c0a9d5d89a6d11e7 to your computer and use it in GitHub Desktop.
Save ramhoj/6dd2c8c9447ff029c0a9d5d89a6d11e7 to your computer and use it in GitHub Desktop.
JSON parse example
{
"status": 1,
"list": {
"229279689": {
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "0",
"status": "0",
"resolved_title": "The Massive Ryder Cup Preview",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"has_video": "1",
"has_image": "1",
"word_count": "3197",
"images": {
"1": {
"item_id": "229279689",
"image_id": "1",
"src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
"width": "0",
"height": "0",
"credit": "Jamie Squire/Getty Images",
"caption": ""
}
},
"videos": [
{
"item_id": "229279689",
"video_id": "1",
"src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
"width": "420",
"height": "315",
"type": "1",
"vid": "Er34PbFkVGk"
}
]
}
}
}
content = File.read("./file.json")
json = JSON.parse(content)
json["status"] # => 1
json["list"]["229279689"]
json["list"].each |key, list| do
key # => 229279689
list["given_url"] # => "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview
list["images"].each |image_key, image| do
image_key # => "1"
image["src"] # => "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
end
list["videos"].each |video| do
video["src"] # => "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
end
list["videos"].each_with_index |video_index, video| do
video_index # => 0,1,2...
video["src"] # => "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment