Hi, guys. Is there a way to merge two org.json.JSO...
# android
n
Hi, guys. Is there a way to merge two org.json.JSONObject using Kotlin? I’ve already googled it, but it was unsuccessfully. Maybe someone has an approach for that? Thanks in advance.
d
Merge how? How do you want to handle duplicate keys?
n
I have predefined keys for my JSON, and they will not duplicate. For example:
Copy code
Json1
{
	"id" : "1",
	"name" : "Example"
}
Json2
{
	"age": "99"
}

Result Json should look like
{
	"id" : "1",
	"name" : "Example",
	"age": "99"
}
s
If you have predefined keys like you mentioned, you can just iterate over the second
JSONObject
and put the values in the first
JSONObject
☝🏼 2
n
Yeah, it’s the first what I was thinking about. I thought maybe there can be more elegant method. Alright, thanks. I will try that one.
554 Views