Decoding JSON Arrays in Scala with Circe

May 27,2025

vlogize

2016-11-23T10:23:24Z

Discover how to efficiently decode JSON arrays using Circe in Scala with clear examples and code snippets. Learn step-by-step to extract data without unnecessary complexity.
---
This video is based on the question https://stackoverflow.com/q/69141540/ asked by the user 'Ry2254' ( https://stackoverflow.com/u/13020185/ ) and on the answer https://stackoverflow.com/a/69142595/ provided by the user 'Boris Azanov' ( https://stackoverflow.com/u/6770614/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to decode array containing json with Circe

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Decoding JSON Arrays in Scala with Circe: A Simple Guide

When working with API responses in Scala, particularly those that return JSON data, decoding the data efficiently is crucial. Among the various libraries available for handling JSON in Scala, Circe stands out because of its powerful features and ease of use. This guide delves into a common problem: decoding an array inside a JSON object using Circe, and we’ll walk through a clear solution step by step.

The Problem

Imagine you have an API response that returns a JSON object encapsulating an array of data. You are interested in extracting certain fields, specifically value and value_classification, from this nested structure. Below is a simplified version of such a JSON response:

[[See Video to Reveal this Text or Code Snippet]]

In this JSON, the fields value and value_classification are nested within an array called data. If you're using Circe, you may find yourself struggling to extract this data effectively without introducing unnecessary complexity or defining multiple classes.

The Solution

Step 1: Understanding the Data Structure

In your initial attempt, you may have defined a Decoder for CryptoData incorrectly, missing the crucial step of navigating through the JSON array. Understand that Circe expects you to use downArray to access elements within an array. Here’s a corrected version of your decoder:

[[See Video to Reveal this Text or Code Snippet]]

Step 2: Define Your Case Classes

To effectively decode the JSON, ensure you have your case classes in place. In this scenario, we have a CryptoData case class as follows:

[[See Video to Reveal this Text or Code Snippet]]

Step 3: Implementing the Sentiment Decoder

To decode the value_classification as an enumerated type (like Fear), define a sealed trait for Sentiment and create an object with the necessary case objects and a decoder:

[[See Video to Reveal this Text or Code Snippet]]

Step 4: Full Implementation

The complete code, including the JSON parsing and the decoding logic, can be structured as:

[[See Video to Reveal this Text or Code Snippet]]

Step 5: Testing the Decoder

You can now test your decoder by passing a JSON string:

[[See Video to Reveal this Text or Code Snippet]]

Final Thoughts

Now you can efficiently decode a JSON array using Circe. By understanding how to navigate through JSON structures and utilizing case classes properly, you can easily access and manipulate complex data formats in Scala. This approach not only simplifies your code but also enhances its readability.

Feel free to apply similar concepts to other structures as needed!

How to decode array containing json with Circejsonscalacircehttp4s circecirce optics