{"id":3617,"date":"2016-04-13T02:55:08","date_gmt":"2016-04-13T02:55:08","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3617"},"modified":"2016-04-13T02:55:08","modified_gmt":"2016-04-13T02:55:08","slug":"scala-read-json-solr","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-read-json-solr\/","title":{"rendered":"Scala: Read JSON from Solr"},"content":{"rendered":"<p>The Scala play library has a library for creating and reading JSON. To import it you can add a couple lines in SBT:<\/p>\n<pre lang=\"scala\">\nlibraryDependencies += \"com.typesafe.play\" %% \"play-json\" % \"2.4.6\"\nlibraryDependencies += \"org.scalaj\" %% \"scalaj-http\" % \"2.3.0\"\n<\/pre>\n<p>To hit Solr, you can build a URL and pull the result with the HTTP library I imported:<\/p>\n<pre lang=\"scala\">\nval url = \n  \"http:\/\/localhost:8983\" +\n  \"\/solr\/ssl_certificates\/\" + \n  \"select?q=level:root\" +\n  \"&rows=83059\n  \"&fl=domain\n  \"&wt=json\"\n\nval result = Http.apply(url)\n  .header(\"Content-Type\", \"application\/json\")\n  .header(\"Charset\", \"UTF-8\")\n  .asString\n  .body\n<\/pre>\n<p>This is an example of what the output looks like:<\/p>\n<pre lang=\"javascript\">\n{\n  \"responseHeader\":{\n    \"status\":0,\n    \"QTime\":1,\n    \"params\":{\n      \"fl\":\"domain\",\n      \"indent\":\"true\",\n      \"q\":\"level:root\",\n      \"wt\":\"json\",\n      \"rows\":\"1\"}\n  },\n  \"response\":{\"numFound\":83059,\"start\":0,\"docs\": [\n      {\n        \"domain\":[\"www.01com.com\"]\n      }\n    ]\n  }\n}\n<\/pre>\n<p>To parse this, you can use an expression with Scala operators resembling an XPath, to get to the area we want, then parse out the important bits. You can match on different types (strings, ints, etc), or use case classes to match as well, if the schema is regular.<\/p>\n<pre lang=\"scala\">\nval jsonResult = Json.parse(result)\nval domains =\n  (jsonResult \\ \"response\" \\ \"docs\").get match {\n    case domainObjList: JsArray =>\n      domainObjList.value.map(\n        (domainObj) =>\n          domainObj match {\n            case obj: JsObject =>\n              obj.value(\"domain\") match {\n                case arr: JsArray => arr(0).get.toString\n              }\n            case _ => ???\n          }\n        )\n      case _ => ???\n    }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Scala play library has a library for creating and reading JSON. To import it you can add a couple lines in SBT: libraryDependencies += &#8220;com.typesafe.play&#8221; %% &#8220;play-json&#8221; % &#8220;2.4.6&#8221; libraryDependencies += &#8220;org.scalaj&#8221; %% &#8220;scalaj-http&#8221; % &#8220;2.3.0&#8221; To hit Solr, you can build a URL and pull the result with the HTTP library I imported: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-read-json-solr\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala: Read JSON from Solr&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,322,480,486,517],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3617"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3617"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3617\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}