{"id":268,"date":"2022-04-25T19:01:05","date_gmt":"2022-04-25T19:01:05","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=268"},"modified":"2022-05-19T21:04:46","modified_gmt":"2022-05-19T21:04:46","slug":"scala-parsing-and-decoding-json-with-circe","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-parsing-and-decoding-json-with-circe\/","title":{"rendered":"Scala &#8211; Parsing and Decoding Json with Circe"},"content":{"rendered":"<p>In this tutorial, you will learn how to parse JSon literals into Circe Json and then use a Decoder to decoder the Circe Json into instances of the case classes.<\/p>\n<p>We would take a number of examples. I also recommend you watch the<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Example 1 &#8211; Personal Data Json Object<\/a><\/li>\n<li><a href=\"#t2\">Create the Case Classes<\/a><\/li>\n<li><a href=\"#t3\">Build Your Decoders<\/a><\/li>\n<li><a href=\"#t4\">Decode Using Your Decoders<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Example 1 &#8211; Personal Data Json<\/strong><\/h4>\n<p>Let&#8217;s assume we have a the json literal below which represents personal data<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  {\r\n    <span style=\"color: #007700;\">\"firstName\"<\/span>: <span style=\"background-color: #fff0f0;\">\"John\"<\/span>,\r\n    <span style=\"color: #007700;\">\"lastName\"<\/span>: <span style=\"background-color: #fff0f0;\">\"Smith\"<\/span>,\r\n    <span style=\"color: #007700;\">\"address\"<\/span>: {\r\n        <span style=\"color: #007700;\">\"street\"<\/span>: <span style=\"background-color: #fff0f0;\">\"45 Downing Street\"<\/span>,\r\n        <span style=\"color: #007700;\">\"city\"<\/span>: <span style=\"background-color: #fff0f0;\">\"New Mexico\"<\/span>,\r\n        <span style=\"color: #007700;\">\"state\"<\/span>: <span style=\"background-color: #fff0f0;\">\"OK\"<\/span>,\r\n        <span style=\"color: #007700;\">\"postCode\"<\/span>: <span style=\"color: #0000dd; font-weight: bold;\">50034<\/span>\r\n    },\r\n    <span style=\"color: #007700;\">\"phoneNumbers\"<\/span>: [<span style=\"background-color: #fff0f0;\">\"02982932\"<\/span>, <span style=\"background-color: #fff0f0;\">\"034832939\"<\/span>, <span style=\"background-color: #fff0f0;\">\"012894837\"<\/span>]\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>From the Json above, you can see that there are a number of types which includes:<\/p>\n<ul>\n<li>String &#8211; the firstName and LastName<\/li>\n<li>Address &#8211; a class with 3 fields<\/li>\n<li>phoneNumbers &#8211; a list of Strings<\/li>\n<\/ul>\n<p>So we would have to create 2 case classes:<\/p>\n<ul>\n<li><strong>PersonaData<\/strong> &#8211; a class that can instantiate the objects that encodable into the Json above<\/li>\n<li><strong>Address<\/strong> &#8211; a class that can instantiate objects encodable into the address block of the Json<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Create the Case Classes<\/strong><\/h4>\n<p>Our case classes are given below:<\/p>\n<p>The Address case class<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Address<\/span><span style=\"color: #333333;\">(<\/span>\r\n                  street<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                  city<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                  state<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                  postCode<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span>\r\n                  <span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The Personal Data case class<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PersonalData<\/span><span style=\"color: #333333;\">(<\/span>\r\n                       firstName<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                       lastName<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                       address<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Address<\/span><span style=\"color: #333333;\">,<\/span>\r\n                       phoneNumber<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">List<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span>\r\n                       <span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Build Your Decoders<\/strong><\/h4>\n<p>Now that we have the case classes, let&#8217;s proceed to build the decoders. We will building a monadic decoder using <em>for comprehension<\/em>.<\/p>\n<p>Let&#8217;s begin by building the addressDecoder. This is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #888888;\">\/\/ Decoder for Address<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">implicit<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> addressDecoder<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Decoder<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Address<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> addressCursor <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      street <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> addressCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"street\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n      city <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> addressCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"city\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n      state <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> addressCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"state\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n      postCode <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> addressCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"postCode\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">yield<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Address<\/span><span style=\"color: #333333;\">(<\/span>street<span style=\"color: #333333;\">,<\/span> city<span style=\"color: #333333;\">,<\/span> state<span style=\"color: #333333;\">,<\/span> postCode<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, we build the personal data decoder the same way using<em> for comprehension<\/em>\u00a0as well.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #888888;\">\/\/ Decoder for PersonalData<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">implicit<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> personalDataDecoder<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Decoder<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">PersonalData<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> personalDataCursor <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      firstName <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> personalDataCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"firstName\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n      lastName <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> personalDataCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"lastName\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n      address <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> personalDataCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Address<\/span><span style=\"color: #333333;\">](<\/span><span style=\"background-color: #fff0f0;\">\"address\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n      phoneNumbers <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> personalDataCursor<span style=\"color: #333333;\">.<\/span>get<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">List<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]](<\/span><span style=\"background-color: #fff0f0;\">\"phoneNumbers\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">yield<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PersonalData<\/span><span style=\"color: #333333;\">(<\/span>firstName<span style=\"color: #333333;\">,<\/span> lastName<span style=\"color: #333333;\">,<\/span> address<span style=\"color: #333333;\">,<\/span> phoneNumbers<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Decode Using our Decoders<\/strong><\/h4>\n<p>Now we can use our decoders. So we can take two steps:<\/p>\n<ul>\n<li><strong>Step 1<\/strong> &#8211; Parse the Json literal into io.circe.json<\/li>\n<li><strong>Step 2<\/strong> &#8211; Decode the Json into an instance of a class<\/li>\n<\/ul>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #888888;\">\/\/ Parse the json literal<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> personalDataLiteralParsed <span style=\"color: #008800; font-weight: bold;\">=<\/span> parse<span style=\"color: #333333;\">(<\/span>personalDataLiteral<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>We have to extract json from the parsed literal because the result of parsing is an Either(parsingFailure, Json). Therefore we simply take the Json value from the right (assuming there&#8217;s no failure, we don&#8217;t try to match the Left). The code is shown below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #888888;\">\/\/ Extract the json from the parsed literal<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> personalDataJson<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Json<\/span> <span style=\"color: #333333;\">=<\/span> personalDataLiteralParsed <span style=\"color: #008800; font-weight: bold;\">match<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Right<\/span><span style=\"color: #333333;\">(<\/span>value<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span>\r\n      value\r\n    <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Left<\/span><span style=\"color: #333333;\">(<\/span>value<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span>\r\n      value<span style=\"color: #333333;\">.<\/span>message<span style=\"color: #333333;\">.<\/span>asJson\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, you can the decode the parsed personal data object using one the two methods given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #888888;\">\/\/Using the personalDataDecoder from the implicit scope<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> personalDataDecoded <span style=\"color: #008800; font-weight: bold;\">=<\/span> personalDataDecoder<span style=\"color: #333333;\">(<\/span>personalDataJson<span style=\"color: #333333;\">.<\/span>hcursor<span style=\"color: #333333;\">)<\/span>\r\n  \r\n  <span style=\"color: #888888;\">\/\/Using the as method<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> personalDataDecoded2 <span style=\"color: #008800; font-weight: bold;\">=<\/span> personalDataJson<span style=\"color: #333333;\">.<\/span>as<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">PersonalData<\/span><span style=\"color: #333333;\">]<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Hint:<\/strong> You can go directly from the original json literal to the PersonalData instance using. In this way you circe, performs both the parsing and decoding in one go.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #008800; font-weight: bold;\">val<\/span> personalDataDecoded3 <span style=\"color: #008800; font-weight: bold;\">=<\/span> decode<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">PersonalData<\/span><span style=\"color: #333333;\">](<\/span>personalDataLiteral<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/youtu.be\/RDzrQBUTdVs\" target=\"_blank\" rel=\"noopener\"><strong>Video Tutorial Here<\/strong><\/a><br \/>\n<iframe loading=\"lazy\" title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/RDzrQBUTdVs\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to parse JSon literals into Circe Json and then use a Decoder to decoder the Circe Json into &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,2],"tags":[41,44,42,51],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-circe","category-scala-programming","tag-circe","tag-decoder","tag-json","tag-parsing"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/268\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}