1

I'm beginner in Circe and I would like retrieve information from this JSon

[  
   {  
      "sha":"7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
      "commit":{  
         "author":{  
            "name":"The Octocat",
            "email":"[email protected]",
            "date":"2012-03-06T23:06:50Z"
         },
         "committer":{  
            "name":"The Octocat",
            "email":"[email protected]",
            "date":"2012-03-06T23:06:50Z"
         },
         "message":"Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file.",
      },
      "url":"https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
   },
   {  
      "sha":"762941318ee16e59dabbacb1b4049eec22f0d303",
      "commit":{  
         "author":{  
            "name":"Johnneylee Jack Rollins",
            "email":"[email protected]",
            "date":"2011-09-14T04:42:41Z"
         },
         "committer":{  
            "name":"Johnneylee Jack Rollins",
            "email":"[email protected]",
            "date":"2011-09-14T04:42:41Z"
         },
         "message":"New line at end of file. --Signed off by Spaceghost",
      },
      "url":"https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
   },
]

I don't understand how this code doesn't catch information about 'author'

val doc= parse(response.json.toString()).getOrElse(Json.Null)
doc.hcursor.downArray.downField("commit").right.as[Seq[String]] match {
   case Left(failure) => println("Fail")
   case Right(json) => println("Ok")
}

Do you have an idea ?

Thank in advance,

1
  • Can you provide some more information? What type is response? What are your imports? Commented Oct 15, 2017 at 2:00

1 Answer 1

3

Your json contains trailing commas in some places. This is against specification.

  val json =
  """[
  {
    "sha":"7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
    "commit":{
      "author":{
      "name":"The Octocat",
      "email":"[email protected]",
      "date":"2012-03-06T23:06:50Z"
    },
      "committer":{
      "name":"The Octocat",
      "email":"[email protected]",
      "date":"2012-03-06T23:06:50Z"
    },
      "message":"Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file."
    },
    "url":"https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d"
  },
  {
    "sha":"762941318ee16e59dabbacb1b4049eec22f0d303",
    "commit":{
      "author":{
      "name":"Johnneylee Jack Rollins",
      "email":"[email protected]",
      "date":"2011-09-14T04:42:41Z"
    },
      "committer":{
      "name":"Johnneylee Jack Rollins",
      "email":"[email protected]",
      "date":"2011-09-14T04:42:41Z"
    },
      "message":"New line at end of file. --Signed off by Spaceghost"
    },
    "url":"https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303"
  }
  ]"""

  case class Author(name: String, email: String, date: String)
  case class Committer(name: String, email: String, date: String)
  case class Commit(author: Author, committer: Committer, message: String)
  case class Record(sha: String, commit: Commit, url: String)

  decode[Seq[Record]](json) match {
      case Right(records) => records.foreach(record => println(record.commit.author))
      case Left(error) => println(error)
  }

//Author(The Octocat,[email protected],2012-03-06T23:06:50Z)
//Author(Johnneylee Jack Rollins,[email protected],2011-09-14T04:42:41Z)

And code like yours works as well:

val doc= parse(json).getOrElse(Json.Null)
doc.hcursor.downArray.downField("commit").downField("author").downField("name").as[String] match {
  case Left(failure) => println(failure)
  case Right(name) => println(name)
}
// The Octocat
Sign up to request clarification or add additional context in comments.

Comments

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.