1

The request https://api.github.com/users returns a list of users. How can I parse it in Scala to array of json objects so I can access objects' fields, e.g., login, id, ...

1 Answer 1

3

you can use sttp-client to make http calls and circe to decode json to scala object.

val SttpVersion = "1.1.12"
val circeVersion = "0.9.3"

libraryDependencies ++= Seq(
  "com.softwaremill.sttp" %% "core" % SttpVersion,
  "com.softwaremill.sttp" %% "async-http-client-backend-future" % SttpVersion,

  "io.circe" %% "circe-core" % circeVersion,
  "io.circe" %% "circe-generic" % circeVersion,
  "io.circe" %% "circe-parser" % circeVersion
)

Example,

object GetExample {

  import scala.concurrent.ExecutionContext.Implicits.global

  import com.softwaremill.sttp._
  import com.softwaremill.sttp.asynchttpclient.future._

  import io.circe._
  import io.circe.parser._

  implicit val backend = AsyncHttpClientFutureBackend()

  def main(args: Array[String]): Unit = {

    val usersResponse: Future[Response[String]] = sttp.get(uri"""https://api.github.com/users""").send()

    val users = usersResponse.map {
      case Response(Right(r), _, _, _, _) => parse(r).map { json =>
        val users: Option[Vector[Json]] = json.asArray.map(arr => arr.flatMap(_.\\("login")))
        users.getOrElse(Vector.empty[Json])
      }
      case Response(Left(l), _, _, _, _) => Vector.empty[Json]
    }

    println(users)
  }
}

you will result as below if you try on REPL

scala>     val users = usersResponse.map {
     |       case Response(Right(r), _, _, _, _) => parse(r).map { json =>
     |         val users: Option[Vector[Json]] = json.asArray.map(arr => arr.flatMap(_.\\("login")))
     |         users.getOrElse(Vector.empty[Json])
     |       }
     |       case Response(Left(l), _, _, _, _) => Vector.empty[Json]
     |     }
users: scala.concurrent.Future[Serializable with Equals] = Future(<not completed>)

scala> users
res1: scala.concurrent.Future[Serializable with Equals] = Future(Success(Right(Vector("mojombo", "defunkt", "pjhyett", "wycats", "ezmobius", "ivey", "evanphx", "vanpelt", "wayneeseguin", "brynary", "kevinclark", "technoweenie", "macournoyer", "takeo", "Caged", "topfunky", "anotherjesse", "roland", "lukas", "fanvsfan", "tomtt", "railsjitsu", "nitay", "kevwil", "KirinDave", "jamesgolick", "atmos", "errfree", "mojodna", "bmizerany"))))
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I got it. I had to add libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.3" % Runtime to build.sbt
It works. But I'm sorry there some output every second. How can I turn it off?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.