Here is my code for merging 2 jsons into one... Is there a better way?
Expected behaviour is to get a 'big json' with sum of 'entry jsons' and no "type-XX" overlaps, and keeping the longest 'rc[ ]' list.
def json_src1 = '''
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
'''
def json_src2 = '''
{
"branch": {
"type-0.3": {
"version": "0.3",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
'''
def concatenateJsons(json_src1, json_src2){
def json = json_src1 + json_src2
return json
}
To change strings to json objects:
json_src1 = new JsonSlurper().parseText(json_src1)
json_src2 = new JsonSlurper().parseText(json_src2)
To concatenate
json = concatenateJsons(json_src1, json_src2)
println JsonOutput.prettyPrint(JsonOutput.toJson(json))
Expected result:
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
"type-0.3": {
"version": "0.3",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
P.S I am losing order of elements inside of Json after merge for no obvious reason : (
"version": "0.3"
moves to the bottom in type-03 {}:
Instead of:
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
"type-0.3": {
"version": "0.3",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
I am getting
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
"type-0.3": {
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]",
"version": "0.3"
}
}
}