Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Last active December 27, 2015 19:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lihaoyi/bb01cfef8510a0adae31 to your computer and use it in GitHub Desktop.
Save lihaoyi/bb01cfef8510a0adae31 to your computer and use it in GitHub Desktop.
Downloading JSON and splatting it into some files in 4 lines of Scala with Ammonite http://lihaoyi.github.io/Ammonite/
haoyi-mbp:Ammonite haoyi$ ~/amm
Loading...
Welcome to the Ammonite Repl 0.5.2
(Scala 2.11.7 Java 1.8.0_25)
haoyi-Ammonite@ load.ivy("org.scalaj" %% "scalaj-http" % "2.2.0")
haoyi-Ammonite@ import ammonite.ops._, scalaj.http._
import ammonite.ops._, scalaj.http._
haoyi-Ammonite@ Http("https://api.github.com/repos/scala/scala").asString
res2: HttpResponse[String] = HttpResponse(
"""
{"id":2888818,"name":"scala","full_name":"scala/scala","owner":{"login":"scala","id":57059,"avatar_url":"https://avatars.githubusercontent.com/u/57059?v=3","gravatar_id":"","url":"https://api.github.com/users/scala","html_url":"https://github.com/scala","followers_url":"https://api.github.com/users/scala/followers","following_url":"https://api.github.com/users/scala/following{/other_user}","gists_url":"https://api.github.com/users/scala/gists{/gist_id}","starred_url":"https://api.github.com/users/scala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scala/subscriptions","organizations_url":"https://api.github.com/users/scala/orgs","repos_url":"https://api.github.com/users/scala/repos","events_url":"https://api.github.com/users/scala/events{/privacy}","received_events_url":"https://api.github.com/users/scala/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/scala/scala","description":"The Scala programming language"...
haoyi-Ammonite@ upickle.json.read(res2.body).asInstanceOf[upickle.Js.Obj]
res3: upickle.Js.Obj = Obj(
ArrayBuffer(
("id", Num(2888818.0)),
("name", Str("scala")),
("full_name", Str("scala/scala")),
(
"owner",
Obj(
ArrayBuffer(
("login", Str("scala")),
("id", Num(57059.0)),
("avatar_url", Str("https://avatars.githubusercontent.com/u/57059?v=3")),
"""
...
haoyi-Ammonite@ for((key, value) <- res3.value) write(cwd/"folder"/key, upickle.json.write(value))
haoyi-Ammonite@ ls! cwd/'folder
res6: LsSeq =
'archive_url 'issues_url
'assignees_url 'keys_url
'blobs_url 'labels_url
'branches_url 'language
'clone_url 'languages_url
'collaborators_url 'merges_url
'comments_url 'milestones_url
'commits_url 'mirror_url
'compare_url 'name
'contents_url 'network_count
'contributors_url 'notifications_url
...
haoyi-Ammonite@ read! cwd/'folder/'archive_url
res7: String = """
"https://api.github.com/repos/scala/scala/{archive_format}{/ref}"
"""
haoyi-Ammonite@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment