title: Response of Ktor failed with exception: kotlinx.serialization.json.JsonDecodingException: Unexpected JSON token at offset 0: Expected '[, kind: LIST'
body: <p>I was developing an App in Kotlin Multiplatform, where I try to implement the regular libraries to networking, and local storage: Ktor with SqlDelight.</p>
  <p>After set up the right configuration to make KMM compile fine, I try to connect with an API, using Ktor library, but although my implementation, returns 200 code response, I get the above title error:</p>
  <pre><code>response failed with exception: kotlinx.serialization.json.JsonDecodingException: Unexpected JSON token at offset 0: Expected '[, kind: LIST'.
  </code></pre>
  <p>Regarding my API.class</p>
  <pre><code>internal val kotlinxSerializer = KotlinxSerializer(
    Json(
      JsonConfiguration(isLenient = false, ignoreUnknownKeys = true, encodeDefaults = false)
    )
  )
  internal val ktorClient = HttpClient {
    defaultRequest {
      header(&quot;Accept-Version&quot;,&quot;v1&quot;)
      header(&quot;Authorization&quot;, &quot;Client-ID $CLIENT_ID&quot;)
    }
    install(JsonFeature) {
      serializer = kotlinxSerializer
    }
    install(Logging) {
      logger = Logger.DEFAULT
      level = LogLevel.ALL
    }
  }
  private const val CLIENT_ID : String = &quot;7DQCWc0Hr_GGIOUeMAVKxqvz9lsVtCpvauRvXJnNq_E&quot;
  internal const val UNSPLASH_URL = &quot;https://api.unsplash.com/search/photos&quot;
  
  //TODO(&quot;La resquest al endpoint se inicializa mal&quot;)
  //TODO(&quot;Tras ejecutar la llamada de red en postman, el error es que no le estoy pasando el access token.&quot;)
  
  /**
  2021-12-18 17:57:25.636 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - REQUEST: https://api.unsplash.com/search/photos?query=query&amp;page=page&amp;per_page=per_page
  2021-12-18 17:57:25.636 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=GET)
  2021-12-18 17:57:25.638 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - COMMON HEADERS
  2021-12-18 17:57:25.638 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - -&gt; Accept: Accept-Version: v1; application/json
  2021-12-18 17:57:25.638 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - -&gt; Accept-Charset: UTF-8
  2021-12-18 17:57:25.639 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - CONTENT HEADERS
  2021-12-18 17:57:25.640 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - BODY Content-Type: null
  2021-12-18 17:57:25.704 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - BODY START
  2021-12-18 17:57:25.705 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient -
  2021-12-18 17:57:25.705 14877-14947/com.jshvarts.kmp.android W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - BODY END
  2021-12-18 17:57:25.974 1547-3158/? D/CompatibilityInfo: mCompatibilityFlags - 0
   */
  
  class UnsplashApi(private val client: HttpClient = ktorClient) {
  
    suspend fun getPets(): List&lt;UnsplashPhoto&gt;? = client.get(UNSPLASH_URL){
      //parameter(&quot;client_id&quot;, CLIENT_ID)
      parameter(&quot;query&quot;, &quot;cats&quot;)
      parameter(&quot;page&quot;, 1)
      parameter(&quot;per_page&quot;, 10)
    }
    //TODO(&quot;Actualizar el endpoint para incorporar la funcionalidad de buscar por nombre&quot;)
    suspend fun getPetsByName(pet: String): List&lt;UnsplashPhoto&gt;? = client.get&lt;List&lt;UnsplashPhoto&gt;&gt;(UNSPLASH_URL){
      parameter(&quot;query&quot;, pet)
      parameter(&quot;page&quot;, 1)
      parameter(&quot;per_page&quot;, 10)
    }
  }
  </code></pre>
  <p>And regarding my repository, where I use the Ktor libraries to recover the data from API, is the following:
  `
  PetsRepository.kt</p>
  <pre><code>internal expect fun cache(): PetsDatabase
  
  class PetsRepository(
    private val api: UnsplashApi,
    private val queries: PetsDBQueries = cache().petsDBQueries
  ) {
    constructor() : this(api = UnsplashApi())
  
    /**
     * If [force] is set to true, attempt to load data from remote api.
     * If remote api is not available. throw [RefreshDataException]
     *
     * If [force] is set to false, attempt to load data from local cache.
     * If local cache is not available, propagate the exception encountered
     */
    fun fetchMembersAsFlow(force: Boolean): Flow&lt;List&lt;Pets&gt;&gt; {
      return if (force) getMembersFromRemote() else getMembersFromCache()
    }
  
    private fun cacheMembers(pets: List&lt;Pets&gt;) {
      queries.deleteAll()
      pets.forEach { pet -&gt;
        queries.insertPet(
            pet.id,
            pet.description,
            pet.url,
            pet.email
        )
      }
    }
    /**
     * Retorna un Flow con una lista de Pets, y guardando en la BD local
     * los pets recuperados de la llamada de red.
     */
    private fun getMembersFromRemote(): Flow&lt;List&lt;Pets&gt;&gt; {
      println(&quot;Getting members from remote&quot;)
  
      return flow {
        val unsplashPhotos = api.getPets()
        Log.d(&quot;info&quot;,  &quot;size pets llamada de red: ${unsplashPhotos!!.size}&quot;)
        Log.d(&quot;info&quot;, &quot;pets $unsplashPhotos&quot;)
        //TODO(&quot;&quot;Convert UnplashPhoto o Pets&quot;)
        var pets : List&lt;Pets&gt; = unsplashPhotos!!.map {
          Pets(it.id!!.toLong(), it.description!!, it.urls.full, it.user.name)
        }
        cacheMembers(pets)
        emit(pets)
      }
          .catch { error(RefreshDataException()) }
          .flowOn(applicationDispatcher)
    }
  
    private fun getMembersFromCache(): Flow&lt;List&lt;Pets&gt;&gt; {
      println(&quot;Getting members from cache&quot;)
      fun loadMembers() = queries.selectAll()
          .executeAsList()
          .map { Pets(id = it.id, description = it.description, url = it.url, email = it.emailUser) }
      return flow { emit(loadMembers()) }
          .catch { error(RefreshDataException()) }
          .flowOn(applicationDispatcher)
    }
  }
  
  </code></pre>
  <p>Regarding my models.</p>
  <p>Pets.kt</p>
  <pre><code>@Serializable
  class Pets (
    @SerialName(&quot;id&quot;) val id: Long,
    @SerialName(&quot;description&quot;)val description: String,
    @SerialName(&quot;url&quot;)val url: String,
    @SerialName(&quot;email&quot;)val email: String
  )
  
  </code></pre>
  <p>UnplashPhoto.kt</p>
  <pre><code>@Serializable
  data class UnsplashPhoto(
    @SerialName (&quot;id&quot;)val id: String?,
    @SerialName (&quot;description&quot;)val description: String?,
    @SerialName (&quot;urls&quot;)val urls: UnsplashPhotoUrls,
    @SerialName (&quot;user&quot;)val user: UnsplashUser
  )
  
    @Serializable
    data class UnsplashPhotoUrls(
      @SerialName(&quot;raw&quot;)  val raw: String,
      @SerialName(&quot;full&quot;)  val full: String,
      @SerialName(&quot;regular&quot;)  val regular: String,
      @SerialName(&quot;small&quot;) val small: String,
      @SerialName(&quot;thumb&quot;)  val thumb: String
    )
  
    @Serializable
    data class UnsplashUser(
      val name: String,
      val username: String,
      val attributionUrl : String
    )
      //get() =
      //  &quot;https://unsplash.com/$username?utm_source=ImageSearchApp&amp;utm_medium=referral&quot;
  
  
  
  </code></pre>
  <p>The other structure is a regular MVVM patern, where I used the ViewModel, to pass the data in form of Flow. But the main point is it seems that Ktor need at some point to deserialize thhe response, and I guess i should be at the UnplashApi.kt level.</p>
  <p>I'm reading by some forus too, and the differents solution they suggest, like put the 'isLenient = false' and 'encodeDefault = false' in the JsonConfiguration of KotlinxSerializer.</p>
  <p>I hope you can help, and if like this take thanks in advance !</p>
  <p>[EDIT]</p>
  <p>After the suggestion of @AlekseiTirman I change the code of my UnplashhApi.kt like this, but although the response code is 200, and the data is loaded correctly to the response, still doesn't work:</p>
  <p>UnplashAPi.kt</p>
  <pre><code>class UnsplashApi(private val client: HttpClient = ktorClient) {      
                                                                        
    suspend fun getPets(): UnsplashReponse? = client.get(UNSPLASH_URL){ 
      //parameter(&quot;client_id&quot;, CLIENT_ID)                               
      parameter(&quot;query&quot;, &quot;cats&quot;)                                        
      parameter(&quot;page&quot;, 1)                                              
      parameter(&quot;per_page&quot;, 10)                                         
    }                                                                   
  </code></pre>
  <p>added 2 pictures of the logcat showing the data:</p>
  <p><a href="https://i.stack.imgur.com/aLIAC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aLIAC.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/9CRnh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9CRnh.png" alt="enter image description here" /></a></p>
  <p>[EDIT]</p>
  <p>After debugging the app, I saw the flow of the data is correct until I try to transform my List of UnsplashPhoto into a List of Pets in the repository class, when I try to map it.</p>