question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to convert JsonObject to Map<String, Any>

See original GitHub issue

What is your use-case and why do you need this feature? I am migrating from GSon to Kotlin.Serialization. One of the uses cases of GSon was used in storing data to Firebase Database.

The class that saves the data does not have the original class/type information, instead it has access to the data as String (json)

The original code will deserialize the String into Map<String, Any>

val map = Gson().fromJson(jsonString, object : TypeToken<HashMap<String, Any>>() {}.type)
ref.setValue(map)

Using Kotlin.Serialization results in Map<String, JsonElement>, and when sent to Firebase, each property becomes an object with content and extra “metadata” field (ie: string=true)

val map = Json.parseToJsonElement(jsonString).jsonObject.toMap()
ref.setValue(map)

Describe the solution you’d like I must be missing something simple, as I am unable to convert JsonObject to Map<String, Any>.

Is there an overload of toMap that would give the expected result?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
ebhsgitcommented, Jun 8, 2021

Thanks for the clarification.

Please consider adding this feature for future release. I believe this is a common use case, especially given Firebase API uses Map<String, Any>

1reaction
sandwwraithcommented, Jun 8, 2021

There’s no built-in method for that, but it can be easily written using recursion. Maybe https://github.com/Kotlin/kotlinx.serialization/issues/746 has some clues

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert JSONObject to Map - java - Stack Overflow
Demarshall the JSON back to an object - in which the Map<String, Object> is demarshalled as a HashMap<String, LinkedHashMap> , by using bjectMapper#readValue()....
Read more >
Convert JSON to Map in Java - Javatpoint
Convert JSON to Map in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, ...
Read more >
Converting JSONObject to HashMap<> - Wajahat Karim
Map <String, Object> mapObj = new Gson().fromJson( myJsonObjectString, new TypeToken<HashMap<String, Object>>() {}.getType() );. This will give ...
Read more >
Convert JSON string to Map - Jackson - Mkyong.com
1. JSON string to Map. JacksonMapExample1.java. package com.mkyong; import com.fasterxml.jackson.databind.ObjectMapper; import java. · 2. Map to ...
Read more >
How to Convert Map to JSON to HashMap in Java?
Here we will be using a method named 'writeValueAsString()' in the code and that can be used to serialize any Java value as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found