Trying to parse a JSON string to Array<Repo>...
# javascript
n
Trying to parse a JSON string to Array<Repo> but end up with this error:
Copy code
Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
What does this error message signify??
Below is the structure of the Repo data class:
Copy code
data class Repo(
    val id: String,
    val name: String,
    val description: String,
    val webUrl: String,
    val totalOpenIssues: Long,
    val buildSystem: BuildSystem = BuildSystem.UNKNOWN,
    val languages: Map<String, Double> = mapOf()
)
Here is some sample output (trimmed for brevity) from the server (written in Kotlin):
Copy code
[ {
  "id" : "3537664",
  "name" : "fdroiddata-localizations",
  "description" : "In order to give translators access to the descriptive text in _fdroiddata_, this project converts it to XLIFF format to sync it with Weblate.  Then there are scripts to convert between the .txt format and the XLIFF format.",
  "webUrl" : "<https://gitlab.com/fdroid/fdroiddata-localizations>",
  "totalOpenIssues" : 0,
  "buildSystem" : "UNKNOWN",
  "languages" : {
    "Python" : 98.54,
    "Shell" : 1.46
  }
}, {
  "id" : "3397954",
  "name" : "fdroid-website-legacy-forum",
  "description" : "This is a wget scrape of <http://f-droid.org|f-droid.org> to get all of the posts from the Wordpress bbpress forum as static HTML",
  "webUrl" : "<https://gitlab.com/fdroid/fdroid-website-legacy-forum>",
  "totalOpenIssues" : 0,
  "buildSystem" : "UNKNOWN",
  "languages" : {
    "HTML" : 99.96,
    "CSS" : 0.04
  }
}, {
  "id" : "3376297",
  "name" : "update-channels",
  "description" : "Libraries to support various update channel mechanisms based on the F-Droid ecosystem.",
  "webUrl" : "<https://gitlab.com/fdroid/update-channels>",
  "totalOpenIssues" : 1,
  "buildSystem" : "GRADLE",
  "languages" : {
    "Java" : 100.0
  }
} ]
This is the line which causes the error:
val repos = JSON.parse<Array<Repo>>("${resp.body}")
Repo is in the common module which is shared with the server, and web-client modules. The server generates a JSON string from Array<Repo>.
d
The error message means it found an 'o' unexpectedly at the second character of your input string. Right? Consider printing it out.
n
Actually the error message was indicating that it can't parse a JSON object.
Managed to get further with parsing the JSON string and printing the first Repo, however this strange error appears:
Copy code
kotlinx-coroutines-core.js:19616 TypeError: Cannot read property 'iterator' of undefined
    at addAll (MutableCollections.kt:127)
    at GroupStatistics.mainLanguage_e0v785$ (_Arrays.kt:8034)
    at updateStatsLayout (main.kt:93)
    at Coroutine$processFetchedRepos$lambda.doResume (main.kt:85)
    at Coroutine$processFetchedRepos$lambda.CoroutineImpl.resumeWith_tl1gpc$ (CoroutineImpl.kt:47)
    at CancellableContinuationImpl.DispatchedTask.run (kotlinx-coroutines-core.js:2682)
    at WindowDispatcher$queue$ObjectLiteral.MessageQueue.process (kotlinx-coroutines-core.js:19877)
    at kotlinx-coroutines-core.js:19846
😕 Appears as though the error comes from the KotlinX Coroutines library.
j
JSON.parse returns a vanilla javascript object with a properties that "accidentally" matches properties of your kotlin class. Also you marked Repo object as "data" class, but that vanilla JS object does not contain any of methods like equals, etc. Languages property won't contain Kotlin Map class but simple js object without any methods. See all in Chrome devtools.