Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

deserialize list of strings into JSON in kotlin

I’m trying to deserialize the following string into JSON

val myList ="""{"user_id": "id1" ,"old_user_ids": "["id1,id2,id3"]", "status":"ACTIVE"}"""

I tried to deserialize using

val record = objectMapper.readValue(myList, UserRecord::class.java)

my data class looks like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data class UserRecord(

    var user_id: String = "",
    var old_user_ids: List<String> = emptyList(),
    var status: String = ""

)

however, I’m getting the following error

Cannot construct instance of `java.util.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[') at [Source: (String)"{"user_id": "id1" ,"old_user_ids": "["id1,id2,id3"]", "status":"ACTIVE"}"; 

I would appreciate if someone can point me to what I’m having wrong.

Output I’m trying to get is

{
 "user_id": "id1",
 "old_user_ids": ["id1","id2","id3"],
 "status":"ACTIVE"
}

>Solution :

Your JSON is invalid, it should be

{"user_id": "id1" ,"old_user_ids": ["id1","id2","id3"], "status":"ACTIVE"}

You have quotes around the [ ] array delimiters which is why it’s choking on that first character. Also your items in the array need quotes around each.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading