Hello everyone, is it possible to get all the keys...
# server
b
Hello everyone, is it possible to get all the keys of nested Json using Kotlin without using actual class, similar to JavaScript?
a
b
Hi Anderson, thanks for the response! Just wondering if this works for the nested object? I don't see any examples?
a
Yes, it does work for nested object. The JSON structure comes out as a kotlin Map object
b
Thanks, if you could provide example given that JSON is like below and wanted to get the keys at
local
level- { "name": "String", "address": { "local" : { "street": "241/80" } } }
🦥 1
Copy code
val map = Mapper.decodeFromString(json)
val address = map["address"] as Map<String,Any>
val local = address["local"] as Map<String,String>
val street = local["street"]
println(street) // "241/80"
a
@Baidyanath what json library are you using?
b
@asad.awadia Klaxon
@andylamax it does not solve the problem.
t
the answer is yes, there are libraries that support that (at least jackson does: https://stackoverflow.com/a/29901800), but why do you not want to make it a class? there are tools for easily creating kotlin classes from json (https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-), if that's the reason
a
Also, maybe state what your expected input is and how you would go expect to go about it to retrieve the keys
b
Okay, so the problem is to find all the keys at any level of nested JSON. In the above example, suppose there are multiple keys and those keys could have another objects and so on. I wanted to extract all the keys of any JSON at any level using Kotlin.
a
I don't think if this will help. But checkout
kotlinx-serialization-properties
b
Thanks Anderson for always the prompt support. Hey @tipsy, thanks for the reply. I'm able to get all the keys in any JSON. Moreover, I'm modifying the algorithm to get all the keys at any given level (key).
🚀 2