Expected start of the array '[', but had 'EOF' instead

Viewed 521

I'm trying to fetch data from API. My app crashes and it gives me this error:

kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead JSON input: { "generated_at": "2022-05-20T10:29:36+00:00", "summaries": [] }

I searched for a solution and found a website that corrects any errors in a json response, but unfortunately it didn't work.

This is my json response (Not the whole response, just one item):

{
"generated_at": "2022-05-20T09:40:26+00:00",
"schedules": [
    {
        "sport_event": {
            "id": "sr:sport_event:27751052",
            "start_time": "2021-08-13T19:00:00+00:00",
            "start_time_confirmed": true,
            "sport_event_context": {
                "sport": {
                    "id": "sr:sport:1",
                    "name": "Soccer"
                },
                "category": {
                    "id": "sr:category:1",
                    "name": "England",
                    "country_code": "ENG"
                },
                "competition": {
                    "id": "sr:competition:17",
                    "name": "Premier League",
                    "gender": "men"
                },
                "season": {
                    "id": "sr:season:83706",
                    "name": "Premier League 21\/22",
                    "start_date": "2021-08-13",
                    "end_date": "2022-05-22",
                    "year": "21\/22",
                    "competition_id": "sr:competition:17"
                },
                "stage": {
                    "order": 1,
                    "type": "league",
                    "phase": "regular season",
                    "start_date": "2021-08-13",
                    "end_date": "2022-05-22",
                    "year": "21\/22"
                },
                "round": {
                    "number": 1
                },
                "groups": [
                    {
                        "id": "sr:league:56620",
                        "name": "Premier League 21\/22"
                    }
                ]
            },
            "coverage": {
                "type": "sport_event",
                "sport_event_properties": {
                    "lineups": true,
                    "extended_player_stats": true,
                    "extended_team_stats": true,
                    "lineups_availability": "pre",
                    "ballspotting": true,
                    "commentary": true,
                    "fun_facts": true,
                    "goal_scorers": true,
                    "scores": "live",
                    "game_clock": true,
                    "deeper_play_by_play": true,
                    "deeper_player_stats": true,
                    "deeper_team_stats": true,
                    "basic_play_by_play": true,
                    "basic_player_stats": true,
                    "basic_team_stats": true
                }
            },
            "competitors": [
                {
                    "id": "sr:competitor:50",
                    "name": "Brentford FC",
                    "country": "England",
                    "country_code": "ENG",
                    "abbreviation": "BRE",
                    "qualifier": "home",
                    "gender": "male"
                },
                {
                    "id": "sr:competitor:42",
                    "name": "Arsenal FC",
                    "country": "England",
                    "country_code": "ENG",
                    "abbreviation": "ARS",
                    "qualifier": "away",
                    "gender": "male"
                }
            ],
            "venue": {
                "id": "sr:venue:53349",
                "name": "Brentford Community Stadium",
                "capacity": 17250,
                "city_name": "London",
                "country_name": "England",
                "map_coordinates": "51.4907295,-0.2891696",
                "country_code": "ENG"
            }
        },
        "sport_event_status": {
            "status": "closed",
            "match_status": "ended",
            "home_score": 2,
            "away_score": 0,
            "winner_id": "sr:competitor:50",
            "period_scores": [
                {
                    "home_score": 1,
                    "away_score": 0,
                    "type": "regular_period",
                    "number": 1
                },
                {
                    "home_score": 1,
                    "away_score": 0,
                    "type": "regular_period",
                    "number": 2
                }
            ]
        }
    }]}

This is some of the data classes:

    @Serializable
data class Schedules(
    @SerialName("generated_at")
    val generatedAt: String,
    @SerialName("schedules")
    val schedules: List<Schedule>
)



    @Serializable
data class Schedule(
    @SerialName("sport_event")
    val sportEvent: SportEvent,
    @SerialName("sport_event_status")
    val sportEventStatus: SportEventStatus
)



@Serializable
data class SportEvent(
    @SerialName("competitors")
    val competitors: List<Competitor>,
    @SerialName("coverage")
    val coverage: Coverage,
    @SerialName("id")
    val id: String,
    @SerialName("sport_event_context")
    val sportEventContext: SportEventContext,
    @SerialName("start_time")
    val startTime: String,
    @SerialName("start_time_confirmed")
    val startTimeConfirmed: Boolean,
    @SerialName("venue")
    val venue: Venue
)

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.a356scoresclone, PID: 19000
kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead
JSON input: {
    "generated_at": "2022-05-20T10:47:43+00:00",
    "summaries": []
}
    at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
    at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
    at kotlinx.serialization.json.internal.JsonLexer.fail(JsonLexer.kt:493)
    at kotlinx.serialization.json.internal.JsonLexer.fail(JsonLexer.kt:215)
    at kotlinx.serialization.json.internal.JsonLexer.unexpectedToken(JsonLexer.kt:198)
    at kotlinx.serialization.json.internal.JsonLexer.consumeNextToken(JsonLexer.kt:188)
    at kotlinx.serialization.json.internal.StreamingJsonDecoder.beginStructure(StreamingJsonDecoder.kt:37)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:29)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
    at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:63)
    at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:32)
    at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100)
    at com.jakewharton.retrofit2.converter.kotlinx.serialization.Serializer$FromString.fromResponseBody(Serializer.kt:30)
    at com.jakewharton.retrofit2.converter.kotlinx.serialization.DeserializationStrategyConverter.convert(DeserializationStrategyConverter.kt:11)
    at com.jakewharton.retrofit2.converter.kotlinx.serialization.DeserializationStrategyConverter.convert(DeserializationStrategyConverter.kt:7)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:520)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
    Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@34fb196, Dispatchers.IO]

Edit: I've just realized that I have the wrong endpoint. So instead of writing "schedules.json" I have "summaries.json". But still I have a similar error:

    kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead
JSON input: .....match_status": "not_started"
            }
        }
    ]
}
2 Answers

you have printed your JSON in log, which is passed to parser

{
    "generated_at": "2022-05-20T10:47:43+00:00",
    "summaries": []
}

your parser was expecting "schedules" array (even empty), but didn't found such key (no array, no object, no value)

so in fact your response doesn't look like posted JSON (which is valid)

I know this doesn't directly answer the question mentioned above but I am posting this here as I had a similar issue during testing on Ktor Server.

This was the error I got.

kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the object '{', but had 'EOF' instead

EOF in JSON just means End of File

This was my code:

fun testFun() = testApplication { ....
val response = client.get("/boruto/heroes")
val actual = Json.decodeFromString<ApiResponse>(response.content.toString())
....
}

I realized the problem occurred as I was using reponse.content instead of response.body.

val actual = Json.decodeFromString<T>(response.content.toString())

I changed it to this and the test eventually passed

val actual = Json.decodeFromString<T>(response.body())

I'm leaving this here in case someone encounters the same issue.

Related